0
SearchCtrl을 테스트하기 위해 Jasmine에서 간단한 테스트를 작성하려고합니다. 그러나 그것을 처음 사용하기 때문에 단위 테스트를 구성하는 방법을 잘 모르겠습니다.단위 테스트에서 TypeError를 반환합니다. undefined가 생성자 오류 메시지가 아닙니다.
function CategoryService($http) {
this.getCategoryList = function() {
return $http({
method: 'GET',
url: server + '/api/category/',
});
}
}
샘플 출력 :
[
{
"id": "1551742a-5963-4d40-9abb-af7a6fb4ec5d",
"category": "Coach"
}
]
controller.js 에서 :
function SearchCtrl($scope, CategoryService) {
CategoryService.getCategoryList().then(function(dataResponse) {
$scope.categories = dataResponse.data;
});
}
services.js에 : 이것은 내가 지금까지 할 수있을 것입니다
있음 search.controller.tests.js :
describe('SearchCtrl', function() {
var $controller, scope, categoryServiceMock;
// TODO: Load the app module
beforeEach(module('app.controllers'));
beforeEach(module('app.services'));
// TODO: Instantiate the controller and mocks
beforeEach(inject(function($controller, categoryServiceMock) {
scope = $rootScope.$new();
// mock categoryService
categoryServiceMock = {
login: jasmine.createSpy('category spy')
};
// instantiate SearchCtrl
controller = $controller('SearchCtrl', {
$scope: scope,
'CategoryService': categoryServiceMock
})
}));
describe('#searchList', function() {
// TODO: Call getCategoryList() on the controller
it('should call getCategoryList on categoryService', function() {
expect(categoryServiceMock.getCategoryList).toHaveBeenCalledWith('Coach');
});
describe('once the search is executed', function() {
it('if successful, return a list of categories', function() {
// TODO: Mock response from CategoryService
expect(categoryServiceMock.getCategoryList).toHaveBeenCalledWith('Coach');
});
});
});
});
이 현재 오류 반환 :
Error: [$injector:unpr] Unknown provider: categoryServiceMockProvider <- categoryServiceMock
가 어떻게 그것을 해결합니까를? 또한, 단위 테스트를 올바르게 구성 했습니까?
PhantomJS 2.1.1 (Windows 8 0.0.0): Executed 0 of 1 SUCC
PhantomJS 2.1.1 (Windows 8 0.0.0) SearchCtrl #searchList should call registerBook Parse Service method FAILED
[email protected]:/myapp-mobile/www/lib/ionic/js/ionic.bundle.js:13691:24
[email protected]:/myapp-mobile/www/lib/ionic/js/ionic.bundle.js:17878:12
[email protected]:/myapp-mobile/www/lib/ionic/js/ionic.bundle.js:17800:30
[email protected]:/myapp-mobile/www/lib/angular-mocks/angular-mocks.js:3074:60
[email protected]://localhost:9876/context.js:151:17
D:/myapp-mobile/www/lib/ionic/js/ionic.bundle.js:17918:53
TypeError: undefined is not a constructor (evaluating 'spyOn(CategoryServiceMock, 'getCategoryList').andCallThrough()') in unit-tests/search.controller.tests.js (line 30)
unit-tests/search.controller.tests.js:30:67
[email protected]://localhost:9876/context.js:151:17
PhantomJS 2.1.1 (Windows 8 0.0.0): Executed 1 of 1 (1 FPhantomJS 2.1.1 (Windows 8 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0.04 secs/0.021 secs)
25 09 2016 09:48:38.940:INFO [watcher]: Changed file "D:/myapp-mobile/www/tests/unit-tests/search.controller.tests.js".
PhantomJS 2.1.1 (Windows 8 0.0.0): Executed 0 of 1 SUCCPhantomJS 2.1.1 (Windows 8 0.0.0) SearchCtrl #searchList should call registerBook Parse Service method FAILED
[email protected]:/myapp-mobile/www/lib/ionic/js/ionic.bundle.js:13691:24
[email protected]:/myapp-mobile/www/lib/ionic/js/ionic.bundle.js:17878:12
[email protected]:/myapp-mobile/www/lib/ionic/js/ionic.bundle.js:17800:30
[email protected]:/myapp-mobile/www/lib/angular-mocks/angular-mocks.js:3074:60
[email protected]://localhost:9876/context.js:151:17
D:/myapp-mobile/www/lib/ionic/js/ionic.bundle.js:17918:53
TypeError: undefined is not a constructor (evaluating 'spyOn(CategoryServiceMock, 'getCategoryList').andCallThrough()') in unit-tests/search.controller.tests.js (line 30)
unit-tests/search.controller.tests.js:30:67
[email protected]://localhost:9876/context.js:151:17
PhantomJS 2.1.1 (Windows 8 0.0.0): Executed 1 of 1 (1 FPhantomJS 2.1.1 (Windows 8 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0.035 secs/0.007 secs)
25 09 2016 09:48:42.307:INFO [watcher]: Changed file "D:/myapp-mobile/www/tests/unit-tests/search.controller.tests.js".
PhantomJS 2.1.1 (Windows 8 0.0.0): Executed 0 of 1 SUCCPhantomJS 2.1.1 (Windows 8 0.0.0) SearchCtrl #searchList should call registerBook Parse Service method FAILED
[email protected]:/myapp-mobile/www/lib/ionic/js/ionic.bundle.js:13691:24
[email protected]:/myapp-mobile/www/lib/ionic/js/ionic.bundle.js:17878:12
[email protected]:/myapp-mobile/www/lib/ionic/js/ionic.bundle.js:17800:30
[email protected]:/myapp-mobile/www/lib/angular-mocks/angular-mocks.js:3074:60
[email protected]://localhost:9876/context.js:151:17
D:/myapp-mobile/www/lib/ionic/js/ionic.bundle.js:17918:53
TypeError: undefined is not a constructor (evaluating 'spyOn(CategoryServiceMock, 'getCategoryList').andCallThrough()') in unit-tests/search.controller.tests.js (line 30)
unit-tests/search.controller.tests.js:30:67
[email protected]://localhost:9876/context.js:151:17
PhantomJS 2.1.1 (Windows 8 0.0.0): Executed 1 of 1 (1 FPhantomJS 2.1.1 (Windows 8 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0.069 secs/0.008 secs)
감사합니다. 완벽하게 작동합니다. – methuselah