在 Jasmine 测试中配置 Angular 服务提供者 [英] Configuring Angular service provider in Jasmine test
问题描述
我的 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
服务.但是,以下代码会产生错误:Error: Unknown provider: 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
服务中配置 options
?
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();
}));
这篇关于在 Jasmine 测试中配置 Angular 服务提供者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!