有没有办法通过服务获得业力单元测试的真实数据?

时间:2015-12-30 09:16:45

标签: angularjs unit-testing karma-jasmine

我想测试角度服务 - 例如:

'use strict';

angular
    .module('com.attributes.services', [])
    .service('krAdminAttributeService', [
        '$rootScope',
        '$http',
        function ($rootScope, $http) {
                var modelData = {
                    "type": "",
                    "groupId": "",
                    "unit": "",
                    "description": "",
                    "name": {}
                };

                var service = {
                    mapAttributes: function (results, update) {
                        if (!update) {
                            modelData.identifier = results.identifier;
                        }
                        modelData.type = results.type;
                        modelData.groupId = results.groupselect;
                        if (results.unit !== undefined) {
                            modelData.unit = results.unit;
                        }

                        modelData.description = results.description;
                        //Name
                        modelData.name = {
                            i18n: true,
                            key: "klapi.attribute:" + results.identifier + ".name"
                        };
                        modelData = angular.copy(modelData);
                    },
                    get: function (params) {
                        //return the promise directly.
                        return $http.get($rootScope.globals.API + 'Attributes', {params: params}).then(function (result) {
                            return result.data;
                        });
                    },
                    getId: function (id) {
                        //return the promise directly.
                        return $http.get($rootScope.globals.API + 'Attributes/' + id + "?filter[include]=group").then(function (result) {
                            return result.data;
                        });
                    },
                    update: function (results) {
                        this.mapAttributes(results, true);
                        return $http.put($rootScope.globals.API + "Attributes/" + results.id, JSON.stringify(modelData), $rootScope.globals.CONFIG).then(function (result) {
                            return result;
                        });
                    },
                    updateName: function (params) {
                        return $http({
                            method: 'POST',
                            url: $rootScope.globals.I18NAPI + "change/de/klapi.attribute",
                            headers: {'Content-Type': 'application/x-www-form-urlencoded'},
                            transformRequest: function (obj) {
                                var str = [];
                                for (var p in obj)
                                    str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                                return str.join("&");
                            },
                            data: params
                        }).then(function (result) {
                            return result;
                        });
                    },
                    add: function (results) {
                        this.mapAttributes(results, false);
                        return $http.post($rootScope.globals.API + "Attributes", JSON.stringify(modelData), $rootScope.globals.CONFIG).then(function (result) {
                            return result;
                        });
                    },
                    delete: function (id) {                        
                        // post(url, data, [config]);
                        return $http.delete($rootScope.globals.API + "Attributes/" + id, $rootScope.globals.CONFIG).then(function (result) {
                            return result;
                        });
                    }
                };
                return service;
            }]);

我的测试看起来像这样:

describe("krAdminAttributeService", function () {
    var krAdminAttributeService,
    $q,
    rootScope,
    httpBackend;



    beforeEach(function () {
        module('app');
        inject(function($httpBackend, _krAdminAttributeService_, _$q_, $rootScope) {
            krAdminAttributeService = _krAdminAttributeService_;
            httpBackend = $httpBackend;
            rootScope = $rootScope.$new();
            $q = _$q_;
        });
    });

    // check to see if it has the expected function
    it('should have a get function', function () {
        expect(angular.isFunction(krAdminAttributeService.get)).toBe(true);
    });
    it('should have a getId function', function () {
        expect(angular.isFunction(krAdminAttributeService.getId)).toBe(true);
    });
    it('should have an update function', function () {
        expect(angular.isFunction(krAdminAttributeService.update)).toBe(true);
    });
    it('should get the attributes', function () {
        var data;
        var response = {id:"12345"};
        httpBackend.whenGET('http://localhost:3000/api/Attributes').respond(200,response);
        // set up a deferred
        var deferred = $q.defer();
        // get promise reference
        var promise = deferred.promise;

        // set up promise resolve callback
        promise.then(function (response) {
            console.log('data',response);
            data = response;
        });
        krAdminAttributeService.get().then(function(response) {
            // resolve our deferred with the response when it returns            
            deferred.resolve(response);
            console.log('response', response);

        });
        httpBackend.flush();

        // make your actual test
        //expect(data).toEqual(200);

    });
});

问题是,这个单元测试不是检索实际数据而只是一些模拟数据(在这种情况下是{id:'12345'})。对我来说,这使得测试不那么有用。有没有办法用真实数据进行测试? (CORS可用)

0 个答案:

没有答案