I have a factory with two functions and two local methods. I have written a Jasmine test case in which I expect the local method updateDetails
and the factory function SavePref.savePref
to be called when
SavePref.saveDetails(values, prop);
is invoked, as shown below:
SavePref.saveDetails(values, prop);
expect(updateDetails).toHaveBeenCalled();
expect(SavePref.savePref).toHaveBeenCalled();
However, when I run the test case, I encounter the following error:
Error: Expected a spy, but got Function.
Can someone please suggest a solution for this?
Here is my factory code:
app.factory('SavePref', function($rootScope, Restangular) {
updateFiles = function(prop) {
if (!prop.found) {
_.merge(prop.oldFiles, prop.newFiles);
}
};
updateDetails = function(values, newProp) {
angular.forEach(values, function(value, index) {
newProp.found = false;
newProp.datas = value.datas;
updateFiles(newProp);
});
};
return {
savePref: function(newProp) {
Restangular.all('rest/savePref').post(newProp).then(function(response) {
$rootScope.success = true;
}, function(errorResponse) {});
},
saveDetails: function(values, prop) {
var newProp = {};
newProp.values= prop.values;
newProp.oldFiles = prop.oldFiles;
newProp.newFiles = prop.newFiles;
updateDetails(values, newProp);
this.savePref(newProp);
}
};
});
My Jasmine Test cases:
describe('Service: SavePref', function(SavePref) {
beforeEach(module('com'));
var httpBackend;
var RestangularMock;
var values;
beforeEach(function() {
values = {
datas: {
file1: 'Test1',
file2: 'Test2',
file3: 'Test3'
}
};
prop = {
oldFiles: 'sampleFile1',
newFiles: 'sampleFile2',
values: {}
};
module(function($provide) {
$provide.value('prop', prop);
});
});
describe('Testing saveDetails Functions', function() {
it('should save details when SaveDetails is called', inject(function(SavePref) {
SavePref.saveDetails(values, prop);
expect(updateDetails).toHaveBeenCalled();
expect(SavePref.savePref).toHaveBeenCalled();
}));
});
});