我在文档中看到了这段代码:
const fixture = MockRender(TargetComponent);
expect(fixture.point.componentInstance).toBeDefined();
但我注意到,我们也可以做
fixture.componentInstance
,所以没有意义。我搜索了文档但没有找到答案。 “.point”到底有什么作用?什么时候该用,什么时候不该用?
Angular 测试中的
.point
属性指的是包裹组件的 DebugElement
。
debugElement
提供对组件模板内的组件、指令和元素的访问。
因此
fixture.point
返回 DebugElement
,而 fixture.componentInstance
返回实际组件实例。
一些主要区别:
fixture.point
允许您使用 .query()
和 .queryAll()
等查询模板中的元素,但您不能使用 fixture.componentInstance
来执行此操作。fixture.componentInstance
允许您直接访问组件类上的方法、属性和其他内容。何时使用
.point
:
何时直接使用
.componentInstance
:
总结一下:
.point
.componentInstace
。我希望这对你有帮助。如果您需要更多说明,请告诉我。干杯🥂