2016-08-22 83 views
2

我在我的角度应用程序中有一个简单的控制器,并且相同的茉莉花测试规范返回参考错误。 我的控制器代码:茉莉花测试中的参考错误:找不到变量

'use strict'; 
angular.module('taskListAppApp') 
    .controller('MainCtrl', function ($scope) { 
    $scope.todoList = [{ 
     todoText: 'In case of Fire', 
     done: false 
    }, { 
     todoText: 'git commit', 
     done: false 
    }, { 
     todoText: 'git push', 
     done: false 
    }, { 
     todoText: 'exit the building!', 
     done: false 
    }]; 


    $scope.getTotalTodos = function() { 
     return $scope.todoList.length; 
    }; 


    $scope.todoAdd = function() { 
     // Checking for null or empty string 
     if (null !== $scope.taskDesc && "" !== $scope.taskDesc) { 
      $scope.todoList.push({ 
       todoText: $scope.taskDesc, 
       done: false 
      }); 
     } 
    }; 

    // Function to remove the list items 
    $scope.remove = function() { 
     var oldList = $scope.todoList; 
     $scope.todoList = []; 
     angular.forEach(oldList, function (x) { 
      if (!x.done) { 
       $scope.todoList.push(x); 
      } 
     }); 
    }; 
}); 

而且我的测试规范:

"use strict" 

    describe('Controller: MainCtrl', function() {  //describe your object type 
     // beforeEach(module('taskListNgApp2App')); //load module 
     beforeEach(angular.mock.module('taskListAppApp')); 
     describe('MainCtrl', function() { //describe your app name 

      var todoCtrl2; 
      beforeEach(inject(function ($controller, $rootScope) { 
       var scope = $rootScope.$new(); 
       todoCtrl2 = $controller('MainCtrl', { 
        //What does this line do? 
        $scope: scope 
       }); 
      })); 

      it('should have todoCtrl defined', function() { 
       expect(todoCtrl2).toBeDefined(); 
      }); 

     it('trial test for toEqual', function(){ 
      var a = 4; 
      expect(a).toEqual(4); 
     }); 
//THESE 2 FAIL 
     it('should have todoList defined', function() { 
      expect(scope.todoList).toBeDefined(); 
     }); 

     it('should have add method defined', function(){ 
      expect(todoCtrl2.todoAdd()).toBeDefined(); 
     }); 

    }); 
}); 

我得到的错误是:

PhantomJS 2.1.1 (Linux 0.0.0) Controller: MainCtrl MainCtrl should have add method defined FAILED 
    TypeError: undefined is not a function (evaluating 'todoCtrl2.todoAdd()') in test/spec/controllers/main.spec.js (line 58) 
    test/spec/controllers/main.spec.js:58:28 
    [email protected]://localhost:8080/context.js:151:17 
PhantomJS 2.1.1 (Linux 0.0.0): Executed 4 of 4 (2 FAILED) (0.05 secs/0.02 secs) 

我尝试过其他方法来调用的对象/功能,但最后2次测试每次都出现相同的错误,即失败。引发ReferenceError

我在哪里呼吁的对象了?

+0

这是因为你正在测试中定义的方法todoAdd返回的值,但我希望的意图是检查方法是在范围界定。因此,尝试如下:它( '应该有定义add方法',函数(){ 预期(todoCtrl2.todoAdd).toBeDefined(); }); – Harpreet

+0

@Harpreet - 我想你的解决方案,用一个沿周杰伦以下建议,但我仍然得到这个错误: PhantomJS 2.1.1(Linux的0.0.0)控制器:MainCtrl MainCtrl应该有add方法定义失败 \t预期不确定被定义为。 –

回答

0

您需要在函数外部声明var scope。您的范围变量在测试用例中未定义。

试试这个

describe('Controller: MainCtrl', function() {  //describe your object type 
    var scope;  
    // beforeEach(module('taskListNgApp2App')); //load module 

    beforeEach(angular.mock.module('taskListAppApp')); 
    describe('MainCtrl', function() { //describe your app name 

     var todoCtrl2; 
     beforeEach(inject(function ($controller, $rootScope) { 
      scope = $rootScope.$new(); 
      todoCtrl2 = $controller('MainCtrl', { 
       //What does this line do? 
       $scope: scope 
      }); 
     })); 

     it('should have todoCtrl defined', function() { 
      expect(todoCtrl2).toBeDefined(); 
     }); 

     it('trial test for toEqual', function(){ 
      var a = 4; 
      expect(a).toEqual(4); 
     }); 
    //THESE 2 FAIL 
     it('should have todoList defined', function() { 
      expect(scope.todoList).toBeDefined(); 
     }); 

     it('should have add method defined', function(){ 
      expect(scope.todoAdd).toBeDefined(); 
     }); 

    }); 
}); 
+0

谢谢Jay!你的解决方案有效。 但是有一个问题,这部分 它('应该有todoList定义',函数(){0}期待(scope.todoList).toBeDefined(); }); 结果在 PhantomJS 2.1.1(Linux 0.0.0)控制器:MainCtrl MainCtrl应该有添加方法定义失败 \t预期未定义将被定义。 那么,如何访问todolist的,如果它被定义检查? –

+0

如果有帮助,你可以接受正确的答案和upvote! –

+0

做了那个.. :)你能帮助其他事情吗? –