Angular testing: testing angular Services with private methods

我的未来我决定 提交于 2019-12-11 11:33:20

问题


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

            };

回答1:


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



来源:https://stackoverflow.com/questions/21230101/angular-testing-testing-angular-services-with-private-methods

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!