我使用 NG-CLI (beta.15) 创建了一个新项目,并修改了app.component.spec
将其更改beforeEach
为 abeforeAll
并导致测试失败并出现以下错误:
失败:无法创建组件 AppComponent,因为它没有导入到测试模块中!
我不明白这个错误是什么意思,当然为什么我会首先得到它。
这是修改后的规格:
import { TestBed, async } from '@angular/core/testing';
import { AppComponent } from './app.component';
describe('App: Ng2CliTest2', () => {
beforeAll(() => {
TestBed.configureTestingModule({
declarations: [
AppComponent
],
});
});
it('should create the app', async(() => {
let fixture = TestBed.createComponent(AppComponent);
let app = fixture.debugElement.componentInstance;
expect(app).toBeTruthy();
}));
it(`should have as title 'app works!'`, async(() => {
let fixture = TestBed.createComponent(AppComponent);
let app = fixture.debugElement.componentInstance;
expect(app.title).toEqual('app works!');
}));
it('should render title in a h1 tag', async(() => {
let fixture = TestBed.createComponent(AppComponent);
fixture.detectChanges();
let compiled = fixture.debugElement.nativeElement;
expect(compiled.querySelector('h1').textContent).toContain('app works!');
}));
});
然后我将规范修改为此,前两个测试通过,第三个测试失败并显示以下消息:
失败:尝试使用被破坏的视图:detectChanges
import { TestBed, async } from '@angular/core/testing';
import { AppComponent } from './app.component';
let fixture;
let app;
describe('App: Ng2CliTest2', () => {
beforeAll(() => {
TestBed.configureTestingModule({
declarations: [
AppComponent
],
});
fixture = TestBed.createComponent(AppComponent);
app = fixture.debugElement.componentInstance;
});
it('should create the app', async(() => {
expect(app).toBeTruthy();
}));
it(`should have as title 'app works!'`, async(() => {
expect(app.title).toEqual('app works!');
}));
it('should render title in a h1 tag', async(() => {
fixture.detectChanges();
let compiled = fixture.debugElement.nativeElement;
expect(compiled.querySelector('h1').textContent).toContain('app works!');
}));
});
我不明白为什么会有任何失败。