I'm trying to add unit tests to my Angular 4 app. In one of my components, there is a link with a (click) handler. When the user clicks the linka function is called which is defined in the .ts class file.
<a id="lnkOperators_lnk_lbl" href="Operator" (click)="onOperatorLinkClick()">Operator</a>
这是用于标识点击事件的测试用例。 在这里,我对如何识别锚标签点击事件有疑问。突出显示的代码不起作用。
it('should', async(() => {
spyOn(component, 'onOperatorLinkClick');
**let link = fixture.debugElement.nativeElement.querySelector('a');**
link.click();
fixture.whenStable().then(() => {
expect(component.onOperatorLinkClick).toHaveBeenCalled();
})
}));
我尝试将其更改为 html 中的按钮,我的规范我的测试用例已通过。
应该对 Angular 应用程序进行单元测试以测试您的打字脚本代码,我的意思是,对于每个孤立的函数,测试某些参数是否给出一些特定结果。
你在那里所做的基本上是在 Angular 中测试事件绑定,我可以向你保证,这不仅仅是经过测试。如果你想测试一些东西,它应该是你自己的"onOperatorLinkClick"函数。所以测试应该是这样的:
it('should', async(() => {
component.onOperatorLinkClick();
fixture.whenStable().then(() => {
// Assure whatever your function should do here
})
}));