角茉莉 - 期望()。tohavebeencalled()不起作用

发布于 2025-02-13 11:10:19 字数 776 浏览 0 评论 0原文

如果有人可以帮助我调试我的单位测试代码,我将不胜感激。基本上,我正在尝试测试在按钮的单击()事件时是否调用了几种方法。它们在实际应用程序上运行良好,但显然不是在测试时:

fit('Cancel button should trigger dialog modal', () => {
    spyOn(_myService, 'showDialogModal');
    spyOn(component, 'confirmCancel');

    const cancelButton = fixture.debugElement.nativeElement.querySelector('.cancelButton');
    cancelButton.click();

    fixture.detectChanges();

    expect(component.confirmCancel).toHaveBeenCalled();
    expect(_myService.showDialogModal).toHaveBeenCalledWith(
      header, message
    );
  });

执行时,我得到了:

expect(spy).toHaveBeenCalled()

    Expected number of calls: >= 1
    Received number of calls:    0

这是用于方法component.confirmcancel(),而第二个是从该函数中调用的。 。

if anyone can help me debug my unit test code I would appreciate it. Basically I'm attempting to test whether a couple of methods are invoked upon a button's click() event. They run fine on the actual app, but apparently not while testing:

fit('Cancel button should trigger dialog modal', () => {
    spyOn(_myService, 'showDialogModal');
    spyOn(component, 'confirmCancel');

    const cancelButton = fixture.debugElement.nativeElement.querySelector('.cancelButton');
    cancelButton.click();

    fixture.detectChanges();

    expect(component.confirmCancel).toHaveBeenCalled();
    expect(_myService.showDialogModal).toHaveBeenCalledWith(
      header, message
    );
  });

Upon execution, I'm getting:

expect(spy).toHaveBeenCalled()

    Expected number of calls: >= 1
    Received number of calls:    0

This is for the method component.confirmCancel(), whereas the second one is called from within that function.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月打碎记忆 2025-02-20 11:10:19

调试的两个想法:

  • 查看STH是否对您的间谍设置​​错误:检查执行测试时是否确实调用了您期望的方法。例如,将Console.Log放入该方法,或放置断点并在调试模式下运行测试。
  • 检查您是否通过'.cancelbutton'查询的元素确实是具有点击侦听器的元素。

如果您还发布了组件的相关HTML和TS代码,则可能会有所帮助。

Two ideas for debugging:

  • To see if sth is wrong about your spy setup: check if the method you expect to be called is actually called when you execute the test. E.g. put a console.log in that method, or put a breakpoint and run the test in debug mode.
  • Check if the element that you query via '.cancelButton' is really the element that has the click listener.

It could be helpful if you also post the relevant html and ts code of the component.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文