我正在编写一个规范,检查在被测 Angular 模块的配置阶段调用的方法。
下面是正在测试的代码的简化视图:
angular.module('core',['services.configAction'])
.config(function(configAction){
configAction.deferIntercept(true);
});
上面发生的事情是我们定义了一个core具有单个依赖项的模块。
然后,在模块的config-block 中core,我们调用给定对象deferIntercept上的方法configAction来使用 from 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);
});
});
问题在于它不会覆盖configAction,因此永远不会调用间谍,原始方法是。如果我将其作为模块
的依赖项删除,它将这样做,因此不会起作用并调用 spy 。coreangular.module('core',[])angular.module('core',['services.configAction'])
知道如何services.configAction在测试期间覆盖而不将其从依赖项列表中删除吗?