gpt4 book ai didi

angular - 如何使用 Jasmine 在 Angular 2 中对警报进行单元测试?

转载 作者:行者123 更新时间:2023-12-02 19:00:44 27 4
gpt4 key购买 nike

我尝试使用 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');
});

最佳答案

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

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

关于angular - 如何使用 Jasmine 在 Angular 2 中对警报进行单元测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52095635/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com