1

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;
                }

            };
4
  • Can you create a plunkr with example code? Commented Jan 22, 2014 at 23:16
  • Can you add example then with anonymous function that works for you? Commented Jan 23, 2014 at 21:50
  • I created a fiddle jsfiddle.net/6aUDu/3 I think this is what you describe, but it seems to work for me. Can you provide an fiddle that breaks? Commented Jan 24, 2014 at 6:27
  • @hassassin, thanks for the effort! I think you are missing something with my question since the fiddle isn't running in an angular unittest (karma/jasmine or otherwise). As I stated in the question - using private methods works in the service It self. the problem was with testing it. Commented Jan 24, 2014 at 13:21

1 Answer 1

3

Answer: This turned to be a completely different problem. Vague karma/chai error messages led me in the wrong direction. The actual problem (if someone bumps into this issue again, which I believe they will..) is the bind method. Turned out the phantomjs```` The headless browser we are running the tests with, Doesn't supportbind``` (as some other es5 methods, not all). Running the same tests with chrome/firefox works perfectly. Once I understood this was the problem, then solving was easy, just added an ES5-shim lib (that included bind) to the karma.conf.js configuration as an included file (or the way you add helpers in any other framework) and everything works perfectlly

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.