Last Updated: February 25, 2016
·
18.85K
· david morrow

Stubbing out confirm dialogs in Jasmine

Ever write a javascript test suite only to have it interrupted by confirm dialogs in your code? Obnoxious huh?

Bypass them by stubbing out with the value you want the user to pick, true/false.

spyOn(window, ‘confirm’).andReturn(false);

or

spyOn(window, 'confirm').andReturn(true);

happy testing...

3 Responses
Add your response

That's exactly what I needed.
God bless you!

over 1 year ago ·

<3

over 1 year ago ·

Just wanted to note that the syntax for this as of Jasmine 2.0 would be:

spyOn(window, 'confirm').and.returnValue(true)

or

spyOn(window, 'confirm').and.returnValue(false)

over 1 year ago ·

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside