经过几个小时的搜索解决方案后,我在测试我的 angular2 应用程序时仍然收到以下错误:
TypeError: Cannot read property 'injector' of null
at TestBed._createCompilerAndModule (webpack:///~/@angular/core/testing/test_bed.js:247:0 <- config/spec-bundle.js:66075:44)
at TestBed._initIfNeeded (webpack:///~/@angular/core/testing/test_bed.js:213:0 <- config/spec-bundle.js:66041:39)
at TestBed.execute (webpack:///~/@angular/core/testing/test_bed.js:275:0 <- config/spec-bundle.js:66103:14)
at Object.<anonymous> (webpack:///~/@angular/core/testing/test_bed.js:367:28 <- config/spec-bundle.js:66195:45)
at ZoneDelegate.invoke (webpack:///~/zone.js/dist/zone.js:332:0 <- config/spec-bundle.js:55179:29)
at ProxyZoneSpec.onInvoke (webpack:///~/zone.js/dist/proxy.js:106:0 <- config/spec-bundle.js:54712:44)
at ZoneDelegate.invoke (webpack:///~/zone.js/dist/zone.js:331:0 <- config/spec-bundle.js:55178:35)
at Zone.run (webpack:///~/zone.js/dist/zone.js:225:0 <- config/spec-bundle.js:55072:44)
at Object.<anonymous> (webpack:///~/zone.js/dist/jasmine-patch.js:123:28 <- config/spec-bundle.js:54391:51)
at ZoneQueueRunner.jasmine.QueueRunner.ZoneQueueRunner.execute (webpack:///~/zone.js/dist/jasmine-patch.js:151:0 <- config/spec-bundle.js:54419:43)
at ZoneQueueRunner.jasmine.QueueRunner.ZoneQueueRunner.execute (webpack:///~/zone.js/dist/jasmine-patch.js:151:0 <- config/spec-bundle.js:54419:43)
at ZoneDelegate.invokeTask (webpack:///~/zone.js/dist/zone.js:365:0 <- config/spec-bundle.js:55212:38)
at Zone.runTask (webpack:///~/zone.js/dist/zone.js:265:0 <- config/spec-bundle.js:55112:48)
at drainMicroTaskQueue (webpack:///~/zone.js/dist/zone.js:497:0 <- config/spec-bundle.js:55344:36)
at ZoneTask.invoke (webpack:///~/zone.js/dist/zone.js:437:0 <- config/spec-bundle.js:55284:26)
这是我的测试文件:
describe('Widget service', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
BaseRequestOptions,
MockBackend,
{
provide: Http, useFactory: (backend: ConnectionBackend, defaultOptions: BaseRequestOptions) => {
return new Http(backend, defaultOptions);
}, deps: [MockBackend, BaseRequestOptions]
},
{provide: WidgetService, useClass: WidgetService}
]
});
});
it('should retrieve widgets by id',
inject([WidgetService, MockBackend], fakeAsync((widgetService: WidgetService, mockBackend: MockBackend) => {
let res: Response;
mockBackend.connections.subscribe(c => {
expect(c.request.url).toBe('src/app/assets/widget_00000011.json');
let response = new ResponseOptions({body: '[{"id": "00000011", "displayName":"My NevisReports Server"}]'});
c.mockRespond(new Response(response));
});
widgetService.getWidgetById('00000011').subscribe((response) => {
res = response;
});
expect(res[0].id).toBe('00000011');
}))
);
});
我的服务如下所示:
@Injectable()
export class WidgetService {
widgets: any[];
baseURL: string = 'http://localhost:8080/patterns/';
constructor(private http: Http) {}
getWidgetById(id: string) {
return this.http.get(`/app/assets/widget_${id}.json`)
.map(
(res:Response) =>
res.json()
);
}
private handleError(error: any) {
console.error('An error occurred', error);
return Promise.reject(error.message || error);
}
}
我也在使用 webpack,我称之为 initTestEnvironment。