Reputation: 3324
I cannot work out how to get Jasmine to check if functions within my testing function are getting called. I know I have to use a spy, but I clearly don't understand the implementation of spies, as it is currently not working.
My function looks something like this:
function templates($rootScope, ShareStats) {
return {
_writeToStore: function(tmpl, tmplOld) {
$rootScope.store.templates.save(tmpl);
if (tmplOld) {
ShareStats.update(tmpl, tmplOld);
} else {
ShareStats.saveAll();
}
},
}
}
My test is looking like this:
describe('Unit: templates', function() {
var Templates,
rootScope,
tmplOld = {...},
tmplNew = {...};
beforeEach(function() {
module('myApp');
inject(function($injector) {...});
});
describe('Templates._writeToStore', function() {
it('should save object to $rootScope.store.templates', function() {
var _writeToStore = Templates._writeToStore(tmplNew, tmplOld);
spyOn(_writeToStore, 'rootScope.store.templates.save');
_writeToStore(tmplNew, tmplOld);
expect(rootScope.store.templates.save).toHaveBeenCalled();
});
it('should call ShareStats.update() if tmplOld is passed', function() {
var _writeToStore = Templates._writeToStore(tmplNew, tmplOld);
spyOn(_writeToStore, 'ShareStats.update');
_writeToStore(tmplNew, tmplOld);
expect(ShareStats.update).toHaveBeenCalled();
});
});
});
Upvotes: 0
Views: 137
Reputation: 4446
You spy isn't quite correct.
spyOn(_writeToStore, 'rootScope.store.templates.save');
should be:
spyOn(rootScope.store.templates, 'save');
ie the object where the function is located as the first argument and the function name as the second.
The problem then is to have a reference to the rootscope
Upvotes: 1