I had some Javascript code and I wanted to make it reusable and testable so I'm trying to make a library out of it. I've already made it in the form of a library but I'm not able to test the constructor using Jasmine.
My library code looks this way:
window.menu = (function(){
function Menu(){
this.addItem('sample');
}
Menu.prototype.addItem(string){
console.log(string);
}
var menu = function(){
return new Menu();
}
return menu;
}());
Now using Jasmine, I wan't to write a test for testing not the content of the addItem function, but the constructor, I just want to know that the addItem function is called.
There is a similar question here, but for some reason is not working for me. If I write this test:
describe("Menu", function() {
it("Test constructor", function() {
spyOn(Menu.prototype,'addItem');
var newMenu = new menu();
}
});
and I always get:
ReferenceError: Can't find variable: Menu in file:///home/whatever/library-test/spec/MenuSpec.js
I don't really know why is this happening, is the test code wrong or is that I've chosen a bad approach for creating my testable library code?