jasmine-fake-window is an extension for Jasmine JavaScript Testing Framework:
- minimally obtrusive utility for handling window mocking in jasmine
- currently only handles window.location
download jasmine-fake-window.js from here and include it in your Jasmine's test runner file (or add it to jasmine.yml file if you're using Ruby with jasmine-gem);
// source file
MyObjectNamespace = {
window: window,
goToUrl: function(url) {
MyObjectNamespace.window.location.href = url
}
}
// MyObjectNamespace spec
describe("MyObjectNamespace.window.location.href", function() {
beforeEach(function() {
MyObjectNamespace.window = jasmine.fakeWindow
});
it('does not change the page location', function() {
MyObject.window.location.href = 'http://www.fake.com';
// this spec would never run otherwise, because it would navigate
// to another page.
expect(MyObject.window.location.href).toEqual('http://www.fake.com');
MyObject.goToUrl('http://www.google.com');
// this spec wouldn't ever run either
expect(MyObject.window.location.href).toEqual('http://www.google.com');
});
});
run grunt
from the command line to lint and run specs
run grunt preprocess
- add navigator functionality
- DRY up code
- add init function to automatically set before and after blocks to reset the location or tie fake window in jasmine.Env