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

を試してみてください。したがって、以下を試してみてください:それは 'メソッドを定義したはずです'、function(){ expect(todoCtrl2.todoAdd).toBeDefined(); }); – Harpreet

+0

@Harpreet - 私はあなたのソリューションを試してみました、に沿って1ジェイは、以下の提案が、私はまだ、このエラーが出る: PhantomJS 2.1.1(Linuxでは0.0.0)コントローラ:MainCtrl MainCtrlは、addメソッドが定義されている必要があります未定義の期待 \tをFAILED定義します。 –

答えて

0

var scopeを機能の外に宣言する必要があります。スコープ変数はテストケースでは未定義です。

はスコープに定義されているあなたが定義するtodoAddメソッドの戻り値をテストしているが、私は意図はその方法を確認することであると思いますので、これはこの

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が定義されている必要があります'、function(){ expect(scope.todoList).toBeDefined(); }); 結果: PhantomJS 2.1.1(Linux 0.0.0)コントローラ:MainCtrl MainCtrlに追加メソッドが定義されている必要があります。FAILED \t未定義の定義が必要です。 todoListにアクセスして、それが定義されているかどうかを確認するにはどうすればよいですか? –

+0

助けがあれば正しいansとupvoteを受け入れることができます! –

+0

それは.. :)あなたは他のもので助けてもらえますか? –

関連する問題