Angular2 testing: What#39;s the difference between a DebugElement and a NativeElement object in a ComponentFixture?(Angular2 测试:ComponentFixture 中的 DebugElement 和 NativeElement 对象有什么区别?)
问题描述
我目前正在整理一些在组件级别测试 Angular 2 应用程序的最佳实践.
I'm currently putting together some best practices for testing Angular 2 apps on a component level.
我看过一些教程查询夹具的 NativeElement 对象以获取选择器等,例如
I've seen a few tutorials query a fixture's NativeElement object for selectors and the like, e.g.
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 的 Angular 2测试种子 她通过父 DebugElement 对象访问 NativeElement.
However, in juliemr's Angular 2 test seed she accesses the NativeElement through a parent DebugElement object.
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?
Are there any specific cases you'd use a fixture's debugElement.nativeElement over its nativeElement?
推荐答案
nativeElement
返回对 DOM 元素的引用DebugElement
是一个 Angular2 类,它包含与调查元素或组件相关的各种引用和方法(参见 DebugNode和DebugElement的源码nativeElement
returns a reference to the DOM elementDebugElement
is an Angular2 class that contains all kinds of references and methods relevant to investigate an element or component (See the source of DebugNode and DebugElement
这篇关于Angular2 测试:ComponentFixture 中的 DebugElement 和 NativeElement 对象有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Angular2 测试:ComponentFixture 中的 DebugElement 和 Nati


- 为什么我的页面无法在 Github 上加载? 2022-01-01
- 我不能使用 json 使用 react 向我的 web api 发出 Post 请求 2022-01-01
- 使用 iframe URL 的 jQuery UI 对话框 2022-01-01
- 如何向 ipc 渲染器发送添加回调 2022-01-01
- 从原点悬停时触发 translateY() 2022-01-01
- 是否可以将标志传递给 Gulp 以使其以不同的方式 2022-01-01
- 为什么悬停在委托事件处理程序中不起作用? 2022-01-01
- 在不使用循环的情况下查找数字数组中的一项 2022-01-01
- 如何显示带有换行符的文本标签? 2022-01-01
- 如何调试 CSS/Javascript 悬停问题 2022-01-01