如何使用Jasmine对angular 2中的alert进行单元测试?
我试图在angular 2中使用Jasmine编写一个警报测试用例。但不管我的测试用例是如何给出错误的,即使我不确定我写的方法是否正确。如果有任何想法,请帮助我 这是我的测试用例:如何使用Jasmine对angular 2中的alert进行单元测试?,angular,unit-testing,karma-jasmine,Angular,Unit Testing,Karma Jasmine,我试图在angular 2中使用Jasmine编写一个警报测试用例。但不管我的测试用例是如何给出错误的,即使我不确定我写的方法是否正确。如果有任何想法,请帮助我 这是我的测试用例: it('正在检查调用警报的showscheduledrequest流',()=>{ 设component=fixture.componentInstance; 组件['ou']='d'; 组件['sen']='ddsd'; var oldalert=警报; oldalert=jasmine.createSpy();
it('正在检查调用警报的showscheduledrequest流',()=>{
设component=fixture.componentInstance;
组件['ou']='d';
组件['sen']='ddsd';
var oldalert=警报;
oldalert=jasmine.createSpy();
组件.handleActionChane('showscheduledrequest');
fixture.autoDetectChanges();
expect(alert).toHaveBeenCalledWith('这不是一个有效的请求');
});代码>您可以尝试监视窗口,然后检查是否已使用例外值调用警报,例如:
it("should call alert", () => {
spyOn(window, "alert");
//your code
expect(window.alert).toHaveBeenCalledWith("expected message");
});