lundi 7 décembre 2015

How to test an angular module with Restangular as a dependency, using Karma/Jasmine

I have a module called campaign service and I have stripped it down to bare bones and still cannot get it to work

'use strict';

angular.module('CampaignService', []).factory('CampaignService', ['$rootScope', '$q', 'Restangular', 'notifications', function ($rootScope, $q, Restangular, notifications) {


    // Private methods.
    function getCampaignsForCstandUser(successCallbackFcn) {
        // defer
        var listDeferred = $q.defer();
        //load
        Restangular.all('campaigns/cstand/' + $rootScope.loggedInUserId).getList().then(function (result) {
            // resolve
            listDeferred.resolve(successCallbackFcn(result));
        }, function (error) {
            // show an error
            notifications.showError('Error while trying to load campaigns. Error:\n' + error);
        });

        return listDeferred.promise;
    };

    // We now return a public API for our service.
    return {
        getCampaignsForCstandUser: getCampaignsForCstandUser
    };
}]);

Here is the unit test file

"use strict";

/*
 * Unit tests for js/service/campaignService.js
 */

    beforeEach(module("CampaignService"));
    beforeEach(module("Restangular"));

    var CampaignService;
    beforeEach(
        inject(function (_CampaignService_) {
                CampaignService = _CampaignService_;
            }
        ));

    describe("main test", function () {
        it("main test", function () {
            expect(CampaignService.getCampaignsForCstandUser()).toBe(true);
        });
    });
});

Everything seems to work up until the point I include the beforeEach(module("restangular"));

Aucun commentaire:

Enregistrer un commentaire