I'm trying to test a function call in the next scenario:
JS:
var Demo = function(option) {
if (option) func();
function func() {
console.log('called')
}
return {
'func': func
}
}
Jasmine:
beforeEach(function() {
var demo = new Demo(true);
spyOn(demo, 'func');
this.demo = demo;
});
it("should call func()", function() {
expect(this.demo.func).toHaveBeenCalled();
});
Despite it logs 'called'
in the console it fails the spec with:
Expected spy func to have been called.
From the code flow I suppose it happens because the spying begins after the function was called. So my question - what is the proper way to capture the function call in the test?