依赖注入在单元测试未定义 [英] Injected Dependency is undefined in Unit Test
问题描述
我新的角度,我不太清楚究竟是如何依赖注入的作品。我的问题是,我有一个服务依赖于服务B,但是当我注入服务A到我的测试服务B是不确定的。
I'm new to Angular and I'm not quite sure exactly how dependency injection works. My problem is that I have Service A which depends on Service B, but when I inject Service A into my test Service B becomes undefined.
我见过<一个href=\"http://stackoverflow.com/questions/14238490/injecting-dependent-services-when-unit-testing-angularjs-services\">Injecting相关的服务时,单元测试AngularJS服务但这个问题比我有点不同。
I have seen Injecting dependent services when unit testing AngularJS services but that problem is a bit different than mine.
这<一个href=\"http://stackoverflow.com/questions/33788405/injected-dependencies-become-undefined-in-angular-service\">Injected依赖成为角服务 undefined是非常相似,我的问题,但我无法翻译JS。
This Injected Dependencies become undefined in angular service is very similar to my issue but I couldn't translate the JS.
我也改变了我的变量名和简化了code去除不相关的线。
I have also changed my variable names and simplified the code to remove irrelevant lines.
Service.ts
export class ServiceA {
constructor(private $resource: ng.resource.IResourceService, private ServiceBInstance: ServiceB){
}
ServiceAMethod() {
var resources = ServiceBInstance.property; //ServiceBInstance is undefined here
}
}
factory.$inject = [
'$resource'
'ServiceBModule'
];
function factory($resource: ng.resource.IResourceService, ServiceBInstance: ServiceB): IServiceA {
return new ServiceA($resource, ServiceBInstance);
}
angular
.module('ServiceAModule')
.factory('ServiceA',
factory);
Spec.ts
describe('ServiceB', (): void => {
beforeEach((): void => {
angular.mock.module(
'ServiceAModule',
'ServiceBmodule',
'ngResource'
);
});
describe('ServiceAMethod', (): void => {
it("should...", inject(
['ServiceA', (ServiceAInstance: ServiceA): void => {
ServiceAInstance.ServiceAMethod(); //Problem Here
}]));
});
});
我遇到的问题是,当我叫ServiceAMethod我得到它规定的错误类型错误:无法读取属性未定义的财产。奇怪的事情是,$资源从来都不是不确定的,但我的ServiceB始终是不确定的。
The problem I'm having is that when I call ServiceAMethod I get an error which states "TypeError: Cannot read property 'property' of undefined". The weird thing is that $resource is never undefined but my ServiceB is always undefined.
我认为,当我注入ServiceA到我的测试中它会自动注入也是其所有依赖。
I thought that when I inject ServiceA into my test it should automatically also inject all of its dependencies.
另外请注意,我不是故意的嘲笑ServiceB。
Also note that I am not mocking ServiceB on purpose.
更新
我也试过注入我ServiceB的一个实例到它但是当我打印出来的变量还指出,它是不确定的,而当我尝试做$ C低于$ C到 beforeEach
I've also tried to inject an instance of my ServiceB into the it but when I print out the variable it also states that is it undefined, and when I try to to do the code below into the beforeEach
inject(function (_ServiceBInstance_: ServiceB) {
console.log(_ServiceBInstance_);
});
我得到和未知提供商错误
推荐答案
好了,所以搜索了几个小时之后,我终于找到了答案。我忘了,包括其中包含的文件角的.config方法
Ok so after hours of searching I finally found the answer. I forgot to include a file which included Angular's .config method
在创建供应商确保你也呼吁的.config,这里有一个例子:
When creating the provider make sure that you also call .config, here's an example:
angular
.module('ServiceBModule')
.config(['ServiceBProvider', (ServiceBClass) => { //Very Important
//Code here
}])
.provider('ServiceB', ServiceB);
这篇关于依赖注入在单元测试未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!