저는 각도 응용 프로그램에 간단한 컨트롤러가 있으며이 코드에 대한 재스민 테스트 스펙은 참조 오류를 반환합니다. 내 컨트롤러 코드 :참조 재스민 테스트에서 참조 오류 : 변수를 찾을 수 없습니다.
'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
어디에서 객체를 호출할까요?
을 시도,하지만 난 의도는 그 방법이 범위에 정의되어 있는지 확인하는 것입니다 바랍니다. 그래서 시도해보십시오 : 그것은 ('정의 된 메소드를 추가해야합니다', function() { expect (todoCtrl2.todoAdd) .toBeDefined(); }); – Harpreet
@Harpreet - 나는 한 제이와 함께 솔루션을 시도 아래의 제안하지만, 난 여전히이 오류를 얻을 : PhantomJS 2.1.1 (리눅스 0.0.0) 컨트롤러 : MainCtrl MainCtrl은 추가 방법은 \t을 실패 정의되지 않은 예상 정의한다 정의 될 수 있습니다. –