Jasmine 단위 테스트에서 AngularJS 모듈 종속성 모의
다른 모듈을 종속성으로 사용하는 모듈 내부의 단위 테스트 컨트롤러 코드를 시도하고 있지만 제대로 조롱하는 방법을 알아낼 수 없었습니다.
Jasmine Framework를 사용하고 있으며 Karma (Testacular)로 테스트를 실행하고 있습니다.
모듈 코드
var app = angular.module('events', ['af.widgets', 'angular-table']);
app.controller('eventsCtrl', function([dependencies]){
$scope.events = [];
...
});
사양 코드
describe('events module', function(){
var $scope,
ctrl;
beforeEach(function(){
angular.mock.module('af.widgets', []);
angular.mock.module('angular-table', []);
module('events', ['af.widgets', 'angular-table']);
});
beforeEach(inject(function($rootScope, $controller){
$scope = $rootScope.new();
ctrl = $controller('NameCtrl', {
$scope: $scope,
});
}));
it('should have an empty events array', function(){
expect($scope.events).toBe([]);
})
});
내가 얻는 오류는 Karma가 "no module af.widgets"이므로 분명히 모듈 종속성을 올바르게 조롱하지 않습니다. 힌트가 있습니까?
하나 이상의 서비스를 선언하는 모듈을 모의하려면이 코드를 사용했습니다.
beforeEach(function(){
module('moduleToMock');
module(function ($provide) {
$provide.value('yourService', serviceMock);
});
});
모의하려는 서비스가 단위 테스트를 원하는 서비스 인 경우에 유용합니다 (다른 jasmine 설명에서). fscof에서 제안한 솔루션은 괜찮지 만 angular-table
모듈에 대한 단위 테스트를 만들 수 없습니다 .
내가 알아 낸 것은 다음과 같습니다.
karma.conf.js 파일에 'angular-table'모듈을로드하지 않았기 때문에 오류가 발생했습니다. 실제 테이블 모듈없이 'events'모듈을 테스트하고 싶었 기 때문에 처음에는 의도적이었습니다.
내 테스트 폴더에 'mocks / angular-table.js'라는 새 파일을 만들고 다음 코드를 추가하여 'angular-table'모듈을 쉽게 모의 할 수있었습니다.
/mocks/angular-table.js
'use-strict';
angular.module('angular-table', []);
이 파일을 내 karma.conf.js 파일에 테스트하고 싶은 실제 'events'모듈과 함께 추가했습니다.
karma.conf.js
...
files = [
JASMINE,
JASMINE_ADAPTER,
'scripts/libs/angular.js',
'scripts/libs/angular-mocks.js',
'scripts/events.js', // this is the real module.
'scripts/mocks/*.js', //loads all custom mocks.
'scripts/specs/*.spec.js' // loads my spec file.
]
...
마지막으로 사양 파일에서 beforeEach 블록에서 별도로 호출하여 두 모듈을 모두 추가 할 수있었습니다.
specs / events.spec.js
beforeEach(function(){
module('angular-table');
module('events');
});
이 게시물 에서 이러한 방식으로 파일을 구조화하는 아이디어를 얻었 습니다.
나는 최근에 AngularJS에서 모의 테스트를 더 쉽게 만들어 줄 ngImprovedTesting을 출시했습니다.
In your case just use the following in your Jasmine test:
beforeEach(ModuleBuilder.forModule('events').serviceWithMocks('eventsCtrl').build());
For more information about ngImprovedTesting check out its introductory blog post: http://blog.jdriven.com/2014/07/ng-improved-testing-mock-testing-for-angularjs-made-easy/
'Programing' 카테고리의 다른 글
Swift IF LET는 어떻게 평가 되나요? (0) | 2020.10.30 |
---|---|
날짜가 1970 년 1 월 1 일부터 계산되는 이유는 무엇입니까? (0) | 2020.10.30 |
Python Pandas : 그룹 별 및 평균? (0) | 2020.10.30 |
MVC ASP.NET에서 HttpContext.Current와 Controller.Context의 차이점 (0) | 2020.10.30 |
파이썬 에그 캐시 (PYTHON_EGG_CACHE) 란 무엇입니까? (0) | 2020.10.30 |