当我尝试在测试中将Injector
传递给beforeEachProviders
时,我收到以下错误。
Failed: Cannot resolve all parameters for 'Injector'(?, ?, ?, ?, ?).
Make sure that all the parameters are decorated with Inject or have valid type annotations and that 'Injector' is decorated with Injectable.
我使用的代码
import { Injector } from 'angular2/core';
describe(() => {
beforeEachProviders(() => [Injector]);
});
缺少什么?我应该给提供商什么能够实例化它?
答案 0 :(得分:0)
我认为您不需要在供应商中配置Injector
进行测试。这个类的一个实例可以直接注入你的测试......
以下是我在其中一个测试中所拥有的内容(没有Injector
的“显式”提供程序),我可以在以后将其注入测试中:
import {it, describe, expect, beforeEach, inject, beforeEachProviders} from 'angular2/testing';
import {HTTP_PROVIDERS, XHRBackend, Response, ResponseOptions} from 'angular2/http';
import {MockBackend, MockConnection} from 'angular2/http/testing';
import {provide, Injector} from 'angular2/core';
import {HttpService} from "./http-service";
describe('HttpService Tests', () => {
beforeEachProviders(() => {
return [
HTTP_PROVIDERS,
provide(XHRBackend, { useClass: MockBackend }),
HttpService
];
});
it('Should return a list of dogs', inject(
[XHRBackend, HttpService, Injector],
(mockBackend, httpService, injector) => {
console.log(injector); // <----- Not null
});
});