2016-08-22 88 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

做了那個.. :)你能幫助其他事情嗎? –