Mocking function that returns promise


// homeController
(function () {
    'use strict';

    angular
        .module('app')
        .controller('homeController', ['restaurantService', homeController]);

    function homeController(restaurantService) {

        // #region viewmodel
        var vm = this;
        vm.restaurants = [];

        // #endregion

        // #region activate
        activate();

        function activate() {
            getRestaurants();
        }
        // #endregion

        // #region internal methods

        function getRestaurants() {
            restaurantService.getRestaurants()
                .then(function (data) {
                    vm.restaurants = data;
                })
                .catch(function (error) {
                    // error
                });
        }

        // #endregion
    }

})();
// unit test
describe('home page', function () {

    var $controller;
    var $q;
    var restaurantService;

    beforeEach(function () {

        // load module
        module('app');

        // overrides for mock injections
        module(function ($provide) {
            // override any dependency here
            // $provide.value('service', 'override'); 

        });

        // initialise
        inject(function (_$controller_, _$q_, _restaurantService_) {
            $controller = _$controller_;
            $q = _$q_;
            restaurantService = _restaurantService_;
        });
    });

    describe('when home controller is initiated', function () {

        it('should load restaurants', function () {

            restaurantService.getRestaurants = function() { return $q.when(['rest1', 'rest2']); }
            var target = $controller('homeController', { restaurantService: restaurantService });

            expect(target.restaurants).toEqual(['rest1', 'rest2']);
        });

    });
});
// unit test
describe('home page', function () {

    var $controller;
    var $q;
    var $rootScope;
    var restaurantService;

    beforeEach(function () {

        // load module
        module('app');

        // overrides for mock injections
        module(function ($provide) {
            // override any dependency here
            // $provide.value('service', 'override'); 

        });

        // initialise
        inject(function (_$controller_, _$q_, _$rootScope_, _restaurantService_) {
            $controller = _$controller_;
            $q = _$q_;
            $rootScope = _$rootScope_;
            restaurantService = _restaurantService_;
        });
    });

    describe('when home controller is initiated', function () {

        it('should load restaurants', function () {

            restaurantService.getRestaurants = function() { return $q.when(['rest1', 'rest2']); }
            var target = $controller('homeController', { restaurantService: restaurantService });
            $rootScope.$digest();

            expect(target.restaurants).toEqual(['rest1', 'rest2']);
        });

    });
 });
comments powered by Disqus