从 pre-RC5 代码更新到 RC6 时,我遇到了类似的问题。为了扩展上面 Joe W 的答案,我替换了以下代码:
import { ReflectiveInjector, provide } from '@angular/core';
import { HTTP_PROVIDERS, RequestOptions } from '@angular/http';
export function main() {
describe('My Test', () => {
let myService: MyService;
beforeAll(() => {
let injector = ReflectiveInjector.resolveAndCreate([
HTTP_PROVIDERS,
provide(RequestOptions, { useValue: getRequestOptions() }),
MyService
]);
myService = injector.get(MyService);
});
it('should be instantiated by the injector', () => {
expect(myService).toBeDefined();
});
...
使用这个 RC6 代码(我猜它也应该适用于 RC5):
import { TestBed } from '@angular/core/testing';
import { HttpModule, RequestOptions } from '@angular/http';
export function main() {
describe('My Test', () => {
let myService: MyService;
beforeAll(() => {
TestBed.configureTestingModule({
imports: [HttpModule],
providers: [
{ provide: RequestOptions, useValue: getRequestOptions() },
MyService
]
});
myService = TestBed.get(MyService);
});
it('should be instantiated by the testbed', () => {
expect(myService).toBeDefined();
});
...