Configuring Angular service provider in Jasmine test

我只是一个虾纸丫 提交于 2019-12-03 17:44:02

问题


I have a service on my someModule module:

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

I am writing a spec, and so far I have the following:

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

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

I need to configure my someService service before each test in my spec. However, the following code produces an error: Error: Unknown provider: someServiceProvider

What am I doing incorrectly? I thought that if I required a module, then any providers available on that module would be 'inherited'? How can I configure the options in my someService service in this test?


回答1:


By the time you call the config function your module is in the run phase. At that point you can no longer inject a provider. Try moving the function that has someServiceProvider injected into it.

beforeEach(module('myModule', function(someProvider) {
    someProvider.configure(1);
}));

it('should work now', inject(function(some) {
    expect(some.func()).toBeAvailable();
}));


来源:https://stackoverflow.com/questions/17323456/configuring-angular-service-provider-in-jasmine-test

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!