Reputation: 476
For the below service factory I want to check if foo() is called or not. And also want to validate the return values of the foo() function. Would anyone tell me how to use spyOn to do this using Jasmine? I am new to the Jasmine framework.
angular.module('myapp').
factory('messageservice', ['$http', '$q', function ($http, $q) {
function foo(query) {
return 'foo';
}
function getServerMessages(query) {
var query=foo();
return $http.get(query))
.then(function (result) {
console.log('success');
}, function (err) {
debugger;
console.log('error');
});
}
return {
getMessages: getFolderMessages,
getMessageDetails: getServerMessages
};
}]);
Thanks,
Krishna
Upvotes: 2
Views: 1467
Reputation: 6066
You cannot do this directly but you can do it indirectly by spying on $http and seeing what is passed as an argument. If you get the right argument, you also know that foo() was called, otherwise it wouldn't have the correct value.
spyOn($http, 'get').andReturn(aPromiseYouShouldResolveAtSomePointInYourTest);
expect($http.get).toHaveBeenCalled();
var queryItWasCalledWith = $http.get.mostRecentCall.args;
// do some assertions on queryItWasCalledWith
Upvotes: 2