问题描述
我有我的 someModule
模块上的服务:
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' });
});
});
我需要在我的规格每个测试之前来配置我的 someService
服务。但是,下面的code产生一个错误:错误:未知提供商:someServiceProvider
I need to configure my someService
service before each test in my spec. However, the following code produces an error: Error: Unknown provider: someServiceProvider
我在做什么错误?我想,如果我需要一个模块,则该模块上提供的任何供应商将被继承?我该如何配置选项
在我的 someService
服务,在本次测试?
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?
推荐答案
通过调用函数配置的模块在运行阶段的时间。在这一点上,你不能再注入一个供应商。尝试移动有someServiceProvider注入到它的功能。
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();
}));
这篇关于在配置测试茉莉花角服务提供商的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!