【发布时间】:2016-10-08 21:42:43
【问题描述】:
我目前正在整理一些在组件级别测试 Angular 2 应用程序的最佳实践。
我看过一些教程查询夹具的 NativeElement 对象以获取选择器等,例如
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!');
});
}));
是否有任何特定情况下您会使用夹具的 debugElement.nativeElement 而不是其 nativeElement?
【问题讨论】:
标签: javascript unit-testing dom angular jasmine