早在 2016 年 6 月,我写了一篇关于如何测试 Angular 2 应用程序的文章。我使用angular2-seed作为起点。
https://raibledesigns.com/rd/entry/testing_angular_2_0_rc1
我决定使用 Angular CLI(来自它的 master 分支)重写本教程,它使用 Angular 2 RC5。我从我的一项测试中看到了一个奇怪的错误。
Error: Token must be defined!
at new BaseException (/Users/mraible/ng2-demo/src/test.ts:1940:23 <- webpack:///Users/mraible/ng2-demo/~/@angular/core/src/facade/exceptions.js:27:0)
at new ReflectiveKey (/Users/mraible/ng2-demo/src/test.ts:27600:19 <- webpack:///Users/mraible/ng2-demo/~/@angular/core/src/di/reflective_key.js:36:0)
at KeyRegistry.get (/Users/mraible/ng2-demo/src/test.ts:27641:22 <- webpack:///Users/mraible/ng2-demo/~/@angular/core/src/di/reflective_key.js:77:0)
at Function.ReflectiveKey.get (/Users/mraible/ng2-demo/src/test.ts:27615:35 <- webpack:///Users/mraible/ng2-demo/~/@angular/core/src/di/reflective_key.js:51:0)
at ReflectiveInjector_.get (/Users/mraible/ng2-demo/src/test.ts:58418:62 <- webpack:///Users/mraible/ng2-demo/~/@angular/core/src/di/reflective_injector.js:586:0)
at NgModuleInjector.get (/Users/mraible/ng2-demo/src/test.ts:40942:52 <- webpack:///Users/mraible/ng2-demo/~/@angular/core/src/linker/ng_module_factory.js:98:0)
at TestBed.get (/Users/mraible/ng2-demo/src/test.ts:11910:47 <- webpack:///Users/mraible/ng2-demo/~/@angular/core/testing/test_bed.js:269:0)
at /Users/mraible/ng2-demo/src/test.ts:11916:61 <- webpack:///Users/mraible/ng2-demo/~/@angular/core/testing/test_bed.js:275:46
at Array.map (native)
at TestBed.execute (/Users/mraible/ng2-demo/src/test.ts:11916:29 <- webpack:///Users/mraible/ng2-demo/~/@angular/core/testing/test_bed.js:275:0)
这是我的测试:
import { provide } from '@angular/core';
import { TestComponentBuilder } from '@angular/compiler/testing';
import { MockActivatedRoute } from '../shared/search/mocks/routes';
import { MockSearchService } from '../shared/search/mocks/search.service';
import { EditComponent } from './edit.component';
import { ActivatedRoute } from "@angular/router";
import { inject } from "@angular/core/testing/test_bed";
describe('Component: Edit', () => {
var mockSearchService:MockSearchService;
beforeEach(() => {
mockSearchService = new MockSearchService();
return [
mockSearchService.getProviders(),
provide(ActivatedRoute, { useValue: new MockActivatedRoute({ 'id': '1' }) })
];
});
it('should fetch a single record', inject([TestComponentBuilder], (tcb:TestComponentBuilder) => {
return tcb.createAsync(EditComponent).then((fixture) => {
let person = {name: 'Emmanuel Sanders', address: {city: 'Denver'}};
mockSearchService.setResponse(person);
fixture.detectChanges();
// verify service was called
expect(mockSearchService.getByIdSpy).toHaveBeenCalledWith(1);
// verify data was set on component when initialized
let editComponent = fixture.debugElement.componentInstance;
expect(editComponent.editAddress.city).toBe('Denver');
// verify HTML renders as expected
var compiled = fixture.debugElement.nativeElement;
expect(compiled.querySelector('h3')).toBe('Emmanuel Sanders');
});
}));
});
我已将此项目发布到 GitHub,因此您可以根据需要重现此问题:https ://github.com/mraible/ng2-demo 。