【问题标题】:How to unit test alert in angular 2 by using Jasmine?如何使用 Jasmine 在 Angular 2 中对警报进行单元测试?
【发布时间】:2018-10-25 20:57:18
【问题描述】:

我试图通过在 Angular 2 中使用 Jasmine 来编写一个警报测试用例。但是无论我的测试用例如何给出错误,即使我不确定我是否写了正确的方法。如果有任何想法,请帮助我。

这是我的测试用例:

it('checking showscheduledrequest flow an alert is called',() =>{
      let component = fixture.componentInstance;
      component['ou'] = 'd';
      component['sen'] = 'ddsd';
      var oldalert = alert;
      oldalert = jasmine.createSpy();
      component.handleActionChane('showscheduledrequest');
      fixture.autoDetectChanges();
      expect(alert).toHaveBeenCalledWith('This is not a valid request');
    });

【问题讨论】:

    标签: angular unit-testing karma-jasmine


    【解决方案1】:

    您可以尝试监视窗口,然后检查是否使用异常值调用了警报,例如:

        it("should call alert", () => {
         spyOn(window, "alert");
         //your code
         expect(window.alert).toHaveBeenCalledWith("expected message");
      });  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-20
      • 1970-01-01
      • 1970-01-01
      • 2016-08-14
      • 2019-02-21
      • 1970-01-01
      相关资源
      最近更新 更多