Reputation: 313
I am trying to spyOn on window.alert.I had spied window's alert function but it still says it has to be spied. My Component has
method1(){
method2();
}
method2{
if(some condition){
alert('hello');
}
}
My unit test :
it('it should say hello', () => {
spyOn(component, 'method1').and.callThrough();
spyOn(window, 'alert');
component.method1();
expect(window.alert).toHaveBeenCalledWith('hello');
}
error is Expected spy alert to have been called with [ 'hello'] but it was never called
Upvotes: 0
Views: 993
Reputation: 348
Because you have spied the method1() and your alert is being called from method2(). So Spying on method2() will pass the test. The following code should run:
it('it should say hello', () => {
spyOn(component, 'method2').and.callThrough();
spyOn(window, 'alert');
component.method1();
expect(window.alert).toHaveBeenCalledWith('hello');
}
Upvotes: 0