问题描述
我创建了一个 ApiService
类,以便能够处理我们的自定义 API 查询,同时使用我们自己的序列化程序和其他功能.
I created an ApiService
class to be able to handle our custom API queries, while using our own serializer + other features.
ApiService
的构造函数签名是:
constructor(metaManager: MetaManager, connector: ApiConnectorService, eventDispatcher: EventDispatcher);
MetaManager
是一个可注入的服务,用于处理 api 的元数据.ApiConnectorService
是一种包装Http
以添加我们的自定义标头和签名系统的服务.EventDispatcher
基本上是 Symfony 的事件调度系统,打字稿.MetaManager
is an injectable service that handles api's metadatas.ApiConnectorService
is a service which is wrappingHttp
to add our custom headers and signature system.EventDispatcher
is basically Symfony's event dispatcher system, in typescript.
当我测试 ApiService
时,我在 beforeEach
中做了一个初始化:
When I test the ApiService
, I do an initialization in beforeEach
:
beforeEach(async(() => {
TestBed.configureTestingModule({
imports : [
HttpModule
],
providers: [
ApiConnectorService,
ApiService,
MetaManager,
EventDispatcher,
OFF_LOGGER_PROVIDERS
]
});
}));
它工作正常.
然后我添加我的第二个规范文件,它是用于 ApiConnectorService
的,带有这个 beforeEach
:
Then I add my second spec file, which is for ApiConnectorService
, with this beforeEach
:
beforeEach(async(() => {
TestBed.configureTestingModule({
imports : [HttpModule],
providers: [
ApiConnectorService,
OFF_LOGGER_PROVIDERS,
AuthenticationManager,
EventDispatcher
]
});
}));
并且所有测试都失败并显示此错误:
And all the tests fail with this error:
错误:无法解析 ApiService 的所有参数:(MetaManager、?、EventDispatcher).
- 如果我从加载的测试中删除
api-connector-service.spec.ts
(ApiConnectorService
的规范文件),ApiService
's 测试将成功. - 如果我从加载的测试中删除
api-service.spec.ts
(ApiService
的规范文件),ApiConnectorService
的测试会成功. - If I remove
api-connector-service.spec.ts
(ApiConnectorService
's spec file) from my loaded tests,ApiService
's tests will succeed. - If I remove
api-service.spec.ts
(ApiService
's spec file) from my loaded tests,ApiConnectorService
's tests will succeed.
为什么会出现这个错误?我的两个文件之间的上下文似乎存在冲突,我不知道为什么以及如何解决此问题.
Why do I have this error? It seems like the context between my two files are in conflict and I don't know why and how to fix this.
推荐答案
这是因为在测试环境中无法从 HttpModule
解析 Http
服务.它取决于平台浏览器.在测试期间,您甚至不应该尝试进行 XHR 调用.
It's because the Http
service can't be resolved from the HttpModule
, in a test environment. It is dependent on the platform browser. You shouldn't even be trying to to make XHR calls anyway during the tests.
出于这个原因,Angular 提供了一个 MockBackend
供 Http
服务使用.我们在测试中使用这个模拟后端来订阅连接,我们可以在每个连接建立时模拟响应.
For this reason, Angular provides a MockBackend
for the Http
service to use. We use this mock backend to subscribe to connections in our tests, and we can mock the response when each connection is made.
这是一个简短的完整示例,您可以使用
Here is a short complete example you can work off of
import { Injectable } from '@angular/core';
import { async, inject, TestBed } from '@angular/core/testing';
import { MockBackend, MockConnection } from '@angular/http/testing';
import {
Http, HttpModule, XHRBackend, ResponseOptions,
Response, BaseRequestOptions
} from '@angular/http';
@Injectable()
class SomeService {
constructor(private _http: Http) {}
getSomething(url) {
return this._http.get(url).map(res => res.text());
}
}
describe('service: SomeService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
{
provide: Http, useFactory: (backend, options) => {
return new Http(backend, options);
},
deps: [MockBackend, BaseRequestOptions]
},
MockBackend,
BaseRequestOptions,
SomeService
]
});
});
it('should get value',
async(inject([SomeService, MockBackend],
(service: SomeService, backend: MockBackend) => {
backend.connections.subscribe((conn: MockConnection) => {
const options: ResponseOptions = new ResponseOptions({body: 'hello'});
conn.mockRespond(new Response(options));
});
service.getSomething('http://dummy.com').subscribe(res => {
console.log('subcription called');
expect(res).toEqual('hello');
});
})));
});
这篇关于测试 - 无法解析 (ClassName) 的所有参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!