Karma test on $q.defer() angularjs with mocked data and asserting state

Posted: February 11, 2015 in web

main controller

angular.module('main.controller', [])
        .controller('MainCtrl', function($scope, Service) {
            Service.getMethod().then(function() {
                Service.getMethod2().then(function() {
                    $scope.detail = Service.getDetail();
                })
            })
        });

service

angular.module('main.services', ['common.localStorage', 'common.configuration'])
        .factory('Service', function($http, configuration, $q) {
            var lists = [];
            var account = null;
            var deffered = $q.defer();
            var aService = {
                getMethod: function() {
                    return $http.get(configuration.getRestUrl(), {secured: true})
                            .success(function(data) {
                                accounts = data;
                                deffered.resolve();
                            });
                    return deffered.promise;
                },
                getMethod2: function() {
                    return $http.get(configuration.getRestUrl() + lists[1].number, {secured: true})
                            .success(function(data) {
                                account = data;
                                deffered.resolve();
                            });
                    return deffered.promise;
                },
                getDetail: function() {
                    return account;
                }
            };
            return aService;
        });

service_spec.js


describe('Service: Main', function () {
    var $httpBackend, restUrl, service;

    beforeEach(module('main', 'mockedData'));

    beforeEach(inject(function (_$httpBackend_, $templateCache, Service, configuration) {
        $httpBackend = _$httpBackend_;
        service = Service;

        restUrl = configuration.getRestUrl();
    }));

    it('should return a promise ', inject(function (mockedLists, mockedData) {
        $httpBackend.whenGET(restUrl).respond(mockedLists);
        $httpBackend.whenGET(restUrl + '333').respond(mockedData);
        
        var lists, account, detail;
        service.getMethod().then(function (data) {
            lists = data;
            service.getMethod2().then(function (data) {
                account = data;
                detail = service.getDetail();
            });
        });
        $httpBackend.flush();
        expect(lists.data.length).toBe(2);
        expect(account.data.number).toBe(22);
        expect(detail.number).toBe(22);
    }));


});

mainCtrl_spec.js


describe('Conrtoller: MainCtrl', function () {
    var $httpBackend, $state, $rootScope, restUrl, mainCtrl;

    beforeEach(module('main', 'mockedData'));

    beforeEach(inject(function (_$rootScope_, _$state_, _$httpBackend_, _$controller_, $templateCache, configuration) {
        $httpBackend = _$httpBackend_;
        $rootScope = _$rootScope_;
        $state = _$state_;
        var $controller = _$controller_;

        mainCtrl = function () {
            return $controller('MainCtrl', {'$scope': $rootScope});
        };
        restUrl = configuration.getRestUrl();
    }));

    afterEach(function () {
        $httpBackend.verifyNoOutstandingExpectation();
        $httpBackend.verifyNoOutstandingRequest();
    });

    it('accounts should be 3', inject(function (mockedLists, mockedData, Service) {
        spyOn(Service, 'getMethod').and.callThrough();
        $httpBackend.whenGET(restUrl).respond(mockedLists);
        $httpBackend.whenGET(restUrl + '33322').respond(mockedData);
        $state.go('app.listAll');
        mainCtrl();
        $httpBackend.flush();
        expect(Service.getMethod).toHaveBeenCalled();
        expect($rootScope.data.number).toBe(34);
    }));
    it('returned to unauthorized', function () {
        $httpBackend.when('GET', restUrl, {secure: true}).respond(401);
        $state.go('app.listAll');
        mainCtrl();
        $httpBackend.flush();
        expect($state.current.name).toBe('app.loginpage');
    });
});
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s