我目前正在整理一些在组件级别测试Angular 2应用程序的最佳实践.
我见过一些教程查询Fixture的NativeElement对象中的 Select 器等,例如
it('should render "Hello World!" after click', async(() => {
builder.createAsync(HelloWorld).then((fixture: ComponentFixture<HelloWorld>) => {
fixture.detectChanges();
let el = fixture.nativeElement;
el.querySelector('h1').click();
fixture.detectChanges();
expect(el.querySelector('h1')).toHaveText('Hello World!');
});
}));
然而,在juliemr's Angular 2 test seed年,她通过父DebugElement对象访问NativeElement.
it('should render "Hello World!" after click', async(() => {
builder.createAsync(HelloWorld).then((fixture: ComponentFixture<HelloWorld>) => {
fixture.detectChanges();
let compiled = fixture.debugElement.nativeElement;
compiled.querySelector('h1').click();
fixture.detectChanges();
expect(compiled.querySelector('h1')).toHaveText('Hello World!');
});
}));
是否有任何特定的情况下,你会使用fixture 的调试元素.nativeElement而不是它的nativeElement?