2016-08-22 3 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 - 나는 한 제이와 함께 솔루션을 시도 아래의 제안하지만, 난 여전히이 오류를 얻을 : PhantomJS 2.1.1 (리눅스 0.0.0) 컨트롤러 : MainCtrl MainCtrl은 추가 방법은 \t을 실패 정의되지 않은 예상 정의한다 정의 될 수 있습니다. –

답변

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

그게 .. :) 다른 일을 도와 줄 수 있니? –

관련 문제