Fra*_*isc 4 unit-testing angularjs angular-providers
我正在编写一个规范来检查在测试的Angular模块的配置阶段调用方法.
这是对正在测试的代码的简化看法:
angular.module('core',['services.configAction'])
.config(function(configAction){
configAction.deferIntercept(true);
});
Run Code Online (Sandbox Code Playgroud)
上面发生的是我们定义一个core具有单一依赖关系的模块.
然后,在模块的config-block中core,我们调用给定使用deferIntercept的configAction对象上的方法services.configAction.
我正在尝试测试core配置调用该方法.
这是当前的设置:
describe('core',function()
{
const configActionProvider={
deferIntercept:jasmine.createSpy('deferIntercept'),
$get:function(){
return {/*...*/}
}
};
beforeEach(function()
{
module(function($provide)
{
$provide.provider('configAction',configActionProvider);
});
module('core.AppInitializer');
inject(function($injector)
{
//...
});
});
it('should call deferIntercept',function()
{
expect(configActionProvider.deferIntercept).toHaveBeenCalledWith(true);
});
});
Run Code Online (Sandbox Code Playgroud)
问题是它没有覆盖configAction,因此从不调用间谍,原始方法是.
如果我将它作为core模块的依赖项删除它将会这样做,因此angular.module('core',[])代替angular.module('core',['services.configAction'])将工作并调用间谍.
有services.configAction没有想过如何在测试期间覆盖而不从依赖列表中删除它?
看看 - https://dzone.com/articles/unit-testing-config-and-run.像下面这样的东西 -
module('services.configAction', function (configAction) {
mockConfigAction = configAction;
spyOn(mockConfigAction, 'deferIntercept').andCallThrough();
});
module('core');
Run Code Online (Sandbox Code Playgroud)
在你的beforeEach可能会完成这项工作.
| 归档时间: |
|
| 查看次数: |
2654 次 |
| 最近记录: |