【问题标题】:Configuring Angular service provider in Jasmine test在 Jasmine 测试中配置 Angular 服务提供者
【发布时间】:2013-06-23 19:00:33
【问题描述】:

我的someModule 模块上有一项服务:

someModule.provider('someService', function() {
    this.options = {};
    this.$get = function () {
        return options;
    };
});

我正在编写规范,到目前为止,我有以下内容:

beforeEach(mocks.module('directives', ['someModule']));

beforeEach(function () {
    directives.config(function (someServiceProvider) {
        someServiceProvider.options({ foo: 'bar' });
    });
});

我需要在我的规范中的每个测试之前配置我的someService 服务。但是,以下代码会产生错误:Error: Unknown provider: someServiceProvider

我做错了什么?我认为如果我需要一个模块,那么该模块上可用的任何提供程序都会被“继承”?在这个测试中,如何在我的someService 服务中配置options?

【问题讨论】:

    标签: unit-testing testing angularjs jasmine


    【解决方案1】:

    当您调用配置函数时,您的模块处于运行阶段。那时您不能再注入提供程序。尝试移动注入了 someServiceProvider 的函数。

    beforeEach(module('myModule', function(someProvider) {
        someProvider.configure(1);
    }));
    
    it('should work now', inject(function(some) {
        expect(some.func()).toBeAvailable();
    }));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-19
      • 2016-10-06
      • 1970-01-01
      • 2017-02-21
      • 1970-01-01
      • 2020-12-21
      • 2017-12-22
      • 2017-01-23
      相关资源
      最近更新 更多