加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 如何模拟配置阶段提供程序进行单元测试?

发布时间:2020-12-17 17:25:38 所属栏目:安全 来源:网络整理
导读:我正在编写一个规范来检查在测试的Angular模块的配置阶段调用方法. 这是对正在测试的代码的简化看法: angular.module('core',['services.configAction']) .config(function(configAction){ configAction.deferIntercept(true); }); 上面发生的是我们定义一
我正在编写一个规范来检查在测试的Angular模块的配置阶段调用方法.

这是对正在测试的代码的简化看法:

angular.module('core',['services.configAction'])
    .config(function(configAction){
        configAction.deferIntercept(true);
    });

上面发生的是我们定义一个具有单一依赖关系的核心模块.
然后,在核心模块的config-block中,我们在configAction对象上调用deferIntercept方法,该方法由services.configAction使用.

我正在尝试测试该核心的配置调用该方法.

这是当前的设置:

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,原始方法是.
如果我将它作为核心模块的依赖项删除它将会这样做,因此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');

在你的beforeEach可能会完成这项工作.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读