在控制器中模拟角度js服务调用

时间:2016-02-16 06:43:15

标签: javascript angularjs karma-jasmine

我有控制器:

(function () {
    'use strict';
    angular.module('myApp').controller('myCtrl', function ($scope, myService) {

        // Start -----> Service call: Get Initial Data
        myService.getInitialData().getData(function (featureManagerdata) {
            var serviceData = featureManagerdata;
        }, function (error) {
            showErrorMessage(error, 'getinitialdata');                
        });
    });
}());

这是我的服务:使用$ resource调用getInitialData并将getData作为自定义函数。

(function () {
    'use strict';
    angular.module('myApp').factory('myService', ['$resource', myService]);

    function myService($resource) {
        var hostUrl = 'http://x.x.x.x/WebAPIDev';

        function getInitialData() {
            var url = hostUrl + '/featuremanager/allfeatures';
            var options = {
                getData: {
                    method: 'GET',
                    isArray: true
                }
            };
            return $resource(url, {}, options);
        );

        return {
            getInitialData: getInitialData
        };
    }
}());

想要使用karma-jasmine来测试控制器中的服务调用:

TestMyCtrl:

describe('Test my controller', function() {
    beforeEach(module('myApp'));

    var scope, Ctrl, service;

    angular.module('myApp').service('mockService', function($resource) {
        getInitialData = function() {
            return {
                'featureId': 1
            };
        }
    });

    beforeEach(inject(function(_$controller_, $rootScope, mockService) {
        scope = $rootScope.$new();
        Ctrl = _$controller_('myCtrl', {
            $scope: scope,
            service: mockService
        });
    }));

    it('should test get initial data', function() {
        var response, mockUserResource, $httpBackend, result;

        service.getInitialData().getData(function(data) {
            response = data;
            // verify data
        });
    });
});

抛出错误,service.getInitialData不是函数。知道为什么它会抛出错误或任何其他更好的方法来测试服务调用

1 个答案:

答案 0 :(得分:1)

在Angular中,factoryservice之间存在一些差异,如果您想了解更多信息,请查看Service vs Factory vs Provider in Angular

其中一个是工厂返回参考对象,服务返回实例

因此,当您尝试在工厂中定义/模拟某个函数或属性时,返回一个带有属性名称和值的Object,但在服务中,您应该将其设置为this < / strong>即可。

除此之外,你应该返回一个定义函数getData的Object,以便在上一个测试用例中调用它。(或者它会抛出 undefine不是函数错误)

因此,请将TestMyCtrl更新为此

describe('Test my controller', function() {
    beforeEach(module('myApp'));

    var scope, Ctrl, service;

    angular.module('myApp').service('mockService', function($resource) {
        //set the function to this.
        this.getInitialData = function() {
            //return {
            //    'featureId': 1
            //};
            //return an object which defined getData function.

            return {
                getData: function(func) {
                    var data = {
                        featureId: 1
                    };
                    //do something here if you want.
                    func(data);
                }
            };
        }
    });

    beforeEach(inject(function(_$controller_, $rootScope, mockService) {
        scope = $rootScope.$new();
        Ctrl = _$controller_('myCtrl', {
            $scope: scope,
            service: mockService
        });
    }));

    it('should test get initial data', function() {
        var response, mockUserResource, $httpBackend, result;

        service.getInitialData().getData(function(data) {
            response = data;
            // verify data
        });
    });
});

希望这可以回答你的问题。 :)