【发布时间】:2019-10-22 13:32:12
【问题描述】:
在这个示例测试文件中,我看到了两种不同的语法
一个是const app = fixture.debugElement.componentInstance;,另一个是const compiled = fixture.nativeElement;,不知道这两种语法有什么不同?
我对角度测试完全陌生,我正在将它应用到我的项目中,但我对此有点困惑。
describe('AppComponent (initial CLI version)', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
AppComponent
],
}).compileComponents();
}));
it('should create the app', async(() => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.debugElement.componentInstance;
expect(app).toBeTruthy();
}));
it(`should have as title 'app'`, async(() => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.debugElement.componentInstance;
expect(app.title).toEqual('app');
}));
it('should render title in a h1 tag', async(() => {
const fixture = TestBed.createComponent(AppComponent);
fixture.detectChanges();
const compiled = fixture.nativeElement;
expect(compiled.querySelector('h1').textContent).toContain('Welcome to app!');
}));
});
【问题讨论】:
标签: angular unit-testing testing angular-cli