lets say I got this kind of method on a service:
this.search = function (term) {
var deferred = $q.defer();
Restangular.all('search').getList(params)
.then(_onRestResult.bind(this, deferred))
.catch(_onRestError.bind(this, deferred));
return deferred.promise;
}
function _onRestResult(deferred, data) { // notice it isn't on the this
//doSomthing
}
But running a basic test.
when I change the then clause to an anonymous function, everything works as expected, but when I used the named private function I get:
karma prints:
TypeError: 'undefined' is not a function (evaluating '_onRestResult.bind(this,deferred)')
I'm aware of this kind of answers
but they refer to controllers and not services (:)) and suggest not using private methods, but we really prefer using them.
I also run into this answer that suggests the private methods are implicitly tested, which was what I thought, until I run into this error messages.
Thanks for the help!
EDIT: I should notice I'm mocking Restangular like this (if this is relevant):
mockRestangular = {
one:function(){
return this;
},
getList:function(calls){
answer ={results:['1','2']}
var deferred = $q.defer();
deferred.resolve(answer);
return deferred.promise;
},
post:function(called){
answer = 'posted: '+called;
var deferred = $q.defer();
deferred.resolve(answer);
return deferred.promise;
},
get: function(called){
answer = this;
var deferred = $q.defer();
deferred.resolve(answer);
return deferred.promise;
},
all:function(){
return this;
}
};
thenwith anonymous function that works for you?