I am trying to test a JavaScript function that uses HTML5 sessionStorage on jasmine-maven-plugin. Jasmine uses HtmlUnit to emulate a web browser.
The problem is that when the automated tests are running during maven build, it says:
- Error: Expected a spy, but got Function.
I have tried this How to deal with sessionStorage locally in FF (for testing) and then I've got this error:
- TypeError: Expected argument of type object, but instead had type object in file: ...
But if I try to run Jasmine on a web page importing jasmine.js, the test works perfectly. I have tried this too Can I access HTML5 storages using HTMLUnit , but without success.
An example of code that works on http://tryjasmine.com/ and not in jasmine-maven-plugin:
function alertItem(id) {
console.log("start");
var x = sessionStorage.getItem(id);
alert(x);
}
describe("sessionStorage test", function () {
console = {
log : function() {},
error : function() {},
warn : function() {}
};
var mockup = function() {
var table = {};
return {
getItem: function(key) {
return table[key];
},
setItem: function(key, value) {
table[key] = value.toString();
},
removeItem: function(key) {
table.pop();
},
clear: function() {
table = {};
}
};
}();
Object.defineProperty(window, 'sessionStorage', {
value: mockup
});
it("must work", function () {
console.log("testing...");
spyOn(sessionStorage, 'getItem').andReturn("my value");
alertItem("id");
expect(sessionStorage.getItem).toHaveBeenCalled();
});
});
Does anyone have an idea?
Thanks.
As @Kyle suggested, using sessionStorage on jasmine-maven-plugin with PhantomJS instead of HtmlUnit worked!
More information at: http://searls.github.io/jasmine-maven-plugin/phantomjs.html
Thank you!