I have the following function
var redirect = function() {
window.location.href = "http://www.google.com";
}
I want to test this function using qUnit.
The problem is, when I call up the HTML document in which my tests run, as soon as it gets to the test that calls redirect()
, the browser loads google.com. What I would like to do is mock out window.location.href somehow so that it doesn't redirect, and so I can check that it was set to the proper value.
Rewriting this in a manner that it would be more testable would be an acceptable answer and is welcomed. Since I am using qUnit, some jQuery magic would be appropriate, as would some old fashioned refactoring. Adding a custom setter for window.location.href was suggested, but I couldn't figure out how to get this to work.
Please, no suggestions of changing the behavior of my code.