mercredi 30 décembre 2015

Is there a way to get the real data for karma unit tests with services?

I would like to test an angular service - e.g.:

'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;
            }]);

My test looks like this:

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

    });
});

The problem is, that this unit test is not retrieving the actual data but just some mocked data (in this case {id:'12345'}). For me, that makes the test less useful. Is there a way to test with real data? (CORS available)

Aucun commentaire:

Enregistrer un commentaire