Like a unit test (using Jasmine) a function in a controller that calls a factory service that returns a promise

In the SampleController below , as a unit test, the postAttributes function calls sampleService.updateMethod . I'm having problems since updateMethod returns a promise.

    angular.module('sampleModule')
       .controller('SampleController', SampleController);

    SampleController.$inject =['sampleService'];

    function SampleController(sampleService){

    this.postAttributes = function() {    
        sampleService.updateMethod(number,attributes)
            .then(function(response){
                //do something on successful update
            },function(response){
                //do something on unsuccessful update
            });
        }; 

    }

Here is the factory service that I have:

    angular.module('sampleModule')
        .factory('sampleService', sampleService);

    sampleService.$inject = ['$http'];

    function sampleService($http) {
        return {
            getMethod: function(acctNumber){
                return $http({
                    method: 'GET',
                    url: //api endpoint
                });
            },
            updateMethod: function(number, attributes){
                return $http({
                    method: 'PUT',
                    url: //api endpoint,
                    data: //payload
                });
            }
        };
    }

I would like to mock the factory service in the controller specification, rather than inject the actual service directly into $ controller, since most unit testing guides test an isolated device.

Controller Specification Example:

    describe('SampleController Test', function(){
        var $controller;
        var service;

        beforeEach(angular.mock.module('sampleModule')); 

        beforeEach(angular.mock.inject(function(_$controller_){
            $controller = _$controller_;
        }));

        it('Testing $scope variable', function(){
            var sampleController = $controller('SampleController', { 
                sampleService: service, //mocked factory service 
            });

            sampleController.postAttributes(); //calling the function first
            //here I would like to make an assertion to check if
            //sampleService.updateMethod has been called with certain parameters                        
            //how do I do that??
        });

    });
+4
2

Google factory , @TehBeardedOne, , .

    describe('SampleController', function(){

        var mockService, controller, deferred, $rootScope;

        beforeEach(function(){

            angular.mock.module('sampleModule');

            angular.mock.module(function($provide){
                $provide.factory('sampleService', function($q){
                    function updateMethod(acct, attr){
                        deferred = $q.defer();
                        return deferred.promise;
                    }
                    return{updateMethod: updateMethod};
                });
            });

            angular.mock.inject(function($controller, sampleService, _$rootScope_){
                $rootScope = _$rootScope_;
                mockService = sampleService;
                spyOn(mockService, 'updateMethod').and.callThrough();
                controller =$controller('SampleController', {
                    sampleService: mockService,
                })
            });
        });

        it('postAttributes function should call updateMethod', function(){

            controller.postAttributes();
            expect(mockService.updateMethod).toHaveBeenCalled();
            expect(mockService.updateMethod).toHaveBeenCalledWith(controller.accountNumber, controller.attributes);
        });

        it('postAttributes success block', function(){
            controller.postAttributes();
            var res = {
                data: '2323'
            }
            deferred.resolve(res);
            $rootScope.$digest();
            expect(//something in success block).toBe(something);
        });

        it('postAttributes failure block', function(){
            controller.postAttributes();
            var res = {
                data: '9898'
            }
            deferred.reject(res);
            $rootScope.$digest();
            expect(controller.lame).toBe('dont type shit');
        });

    });

sampleService $provider updateMethod, $q. .

+3

, , . , , , , . $q $rootScope, , .

, , , , , . . , postAttributes() , . , . - .

describe('SampleController Test', function(){
    var $controller;
    var service;
    var $q;
    var $rootScope;
    var updateMethodDeferred;
    var ctrl;
    var mockHttpObject;

    beforeEach(angular.mock.module('sampleModule'));

    beforeEach(angular.mock.inject(function(_sampleService_, _$q_, _$rootScope_, _$controller_){
        service = _sampleService_;
        $q = _$q_;
        $rootScope = _$rootScope_;
        $controller = _$controller_;

        mockHttpObject = {
            //add mock properties here
        }

        updateMethodDeferred = $q.defer();
        spyOn(service, 'updateMethod').and.returnValue(updateMethodDeferred.promise);

        ctrl = $controller('SampleController', {
            sampleService: service, //mocked factory service
        });
    }));

    it('Testing $scope variable', function(){
        ctrl.postAttributes();
        expect(service.updateMethod).toHaveBeenCalled();
    });

    it('Should do something after promise is returned', function(){
        ctrl.postAttributes();
        updateMethodDeferred.resolve(mockHttpObject);
        $rootScope.$digest();
        expect(/*something that happened on successful update*/).toBe(/*blah*/)           
    });
});
+1

All Articles