Ad

Jasmine Return Rejected In A Mocked Factory's Promise

My controller's init:

    var init = function () {
        httpUsers.getUsers().then(function (data) {
            $timeout(function() {
                // Do something...
            });
        }).catch(function (error) {
            vm.error = "Me!";
            toastr.error(error);
        });
    }

My mocked service:

beforeEach(function() {
    module('httpService', function($provide) {
        $provide.factory('httpUsers', function () {

            var service = {
                getUsers: getUsers
            }

            function getUsers() {
                    deferred = $q.defer(); // deferred is a global variable
                    deferred.resolve([
                        { id: 1, firstName: "John", lastName: "Doe" },
                        { id: 2, firstName: "Jane", lastName: "Doe" },
                        { id: 3, firstName: "Jack", lastName: "Doe" }
                    ]);
                    return deferred.promise;
            };

            return service;
        });
    });
});

Everything works fine for returning the resolved promise.

However, I want to test the catch clause in my controller's init function.

I've tried this:

describe('init', function() {
    it('should throw error', function () {
        deferred.reject('testing');
        expect(controller.error).toBe("Error!");  // NOTE: 'vm' is set to 'this', so controller is 'vm'
    });
});

But all I get is:

Expected undefined to be 'Error!'.

I don't get 'testing', 'Me!', or 'Error!'. How do I force a rejection on the promise from my test?

Thanks!

EDIT
Here's my controller injection:

beforeEach(inject(function(_$controller_, _$rootScope_, _$q_, _$timeout_, _global_) {

    $rootScope = _$rootScope_;
    $scope = _$rootScope_.$new();
    $q = _$q_;
    $timeout = _$timeout_;
    global = _global_;

    controller = _$controller_('usersController', {
        $rootScope: $rootScope,
        $scope: $scope,
        $q: $q
    });

    $timeout.flush();
}));
Ad

Answer

something like below: - Your mock service

beforeEach(function() {
    module('httpService', function($provide) {
        $provide.factory('httpUsers', function () {

            var service = {
                getUsers: getUsers
            }

            function getUsers() {
                deferred = $q.defer(); // deferred is a global variable
                return deferred.promise;
            };

            return service;
        });
    });
});

your test for rejection

describe('init', function() {
    it('should throw error', function () {
        deferred.reject('testing');
        $scope.$apply();
        expect(controller.error).toBe("Error!"); 
    });
});

your test for resolve

describe('init', function() {
    it('should throw error', function () {
        deferred.resolve([
                    { id: 1, firstName: "John", lastName: "Doe" },
                    { id: 2, firstName: "Jane", lastName: "Doe" },
                    { id: 3, firstName: "Jack", lastName: "Doe" }
                ]);
        $scope.$apply();
        $timeout.flush();
        expect(controller.error).toBe("Error!"); 
    });
});
Ad
source: stackoverflow.com
Ad