由网友(污味吻)分享简介:我有以下问题:假设我有模块angular.module('myModule',['$strap'] );//module depends on AngularStrapMymodule中已定义的服务如下:myModule has service defined as following : angular.mo...
我有以下问题:
假设我有模块
angular.module('myModule',['$strap'] );//module depends on AngularStrap
Mymodule中已定义的服务如下:
myModule has service defined as following :
angular.module('myModule').service('myModuleService', ['$modal', function ($modal)
{ ....
}); //depends on $modal service from AngularStrap
现在我需要测试myModuleService,我需要模拟模式$服务在试验(茉莉为主)myModuleService加载如下
Now I need to test myModuleService and I need to mock $modal service In test ( jasmine based ) myModuleService loaded as following
var modalMock = jasmine.createSpy();
..
angular.mock.module('myModule');
...
$provide.value('$modal', modalMock);
..
angular.mock.inject(function ($injector) {
// Initialize the service under test instance
myService = $injector.get('myModuleService');
});
问题是,当myModuleService被加载时,也是真实的$模式的业务加载和NOT嘲弄$模态。结果现在的问题是:什么是缺少在这里,以便与mockModal加载为myService,而不是与实际$模式?在此先感谢
The problem is when myModuleService is loaded , also real $modal service is loaded and NOT mock of the $modal. The question is: what is missing here in order to load myService with mockModal and not with real $modal ? Thanks in advance
推荐答案
我失去了你的一些测试实施细节,但以下工作:
I'm missing some of your test implementation details, but the following works:
describe('mock modal', function () {
var modalMock = jasmine.createSpyObj('modal', ['show']),
service;
beforeEach(function () {
angular.mock.module('myModule');
angular.mock.module(function ($provide) {
$provide.value('$modal', modalMock);
});
angular.mock.inject(function (myModuleService) {
// Initialize the service under test instance
service = myModuleService;
});
});
it('mocks modal', function () {
service.foo();
expect(modalMock.show).toHaveBeenCalled();
});
});
您可以找到完整的小提琴这里
相关推荐
最新文章