Is it possible to mock local variable in angularjs factory from karma?

久未见 提交于 2020-01-03 03:14:07

问题


I have the below code:

'use strict';

angular
    .module('testmodule')
    .factory('TestService', ['$q', '$timeout',
        function ($q, $timeout) {

var timeoutRetries = 0; // need to mock this from here

            var api = new TestApi();

            function getResults(id, prevDeferred) {
                var deferred = prevDeferred || $q.defer();

                function handleSuccessResponse(data) {
                    if (data.status === 'ready') {
                        results.put(id, data);
                        deferred.resolve(data);
                    } else {                        

if (++timeoutRetries > 30) { // It wont get in here

                            handleErrorResponse();
                        } else {
                            $timeout(function () {
                                getResults(id, deferred);
                            }, 2000);
                        }
                    }
                }

                function handleErrorResponse(response) {
                    deferred.reject(response);
                }

                if (results.get(id)) {
                    deferred.resolve(doSomething.get(id));
                    return deferred.promise;
                }

                api.get({id: id}).then(handleSuccessResponse, handleErrorResponse);
                return deferred.promise;
            }

            return {
                getResults: getResults
            };
        }]);

I am trying to mock the timeoutRetries entry from karma but i am not able to do it. Is it the ideal way of declaring it or should i want to move the variable to some function and update or which is the best way to mock it up from karma?

Tried with inject, declared the variable before calling the function. Still no success.


回答1:


You need to cover that branch so that it automatically covers local variable. Looks like your test is not covering that scenario.

You are depend on status data from results.. if it is ready you are returning results.. if not increment timeout and if it is > 30 then throw error message else poll for the results again until either you get status as ready or timeout is 30.

You can cover else branch easily.

var isReadyTrue = false;
$httpBackend.expectGET(url).respond(function () {
            return [201, function(){
if (isReadyTrue) {
results.data = 'Ready';
} else {
results.data = 'Not Ready';
}
isReadyTrue = true; // Next result will be ready..

return results;
}];
        });

We can't mock local variable, but we can mock scenario to cover that.

First result will be not Ready so timeout will be 1 and goes for polling, Next result returns with Ready data.

You can mock for timeout 30 using combination of above and $timeout



来源:https://stackoverflow.com/questions/37921670/is-it-possible-to-mock-local-variable-in-angularjs-factory-from-karma

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