问题
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