dimanche 3 janvier 2016

Injecting $stateParams for angular unit testing testing with karma

Lets start with the controller code:

angular
.module('hc.hotelContent')
.controller('NameLocationController', nameLocationCtrlFn); //Todo change hotelDataService
nameLocationCtrlFn.$inject = ['$stateParams', 'hotelDataService'];

  function nameLocationCtrlFn($stateParams, hotelDataService) {
       var vm = this,
       hotelId = $stateParams.hotelId;
       vm.Keys = {
        Name: 'Name',
        Street: 'Street',
        City: 'City',
        State: 'State',
        Zip: 'Zip',
        Country: 'Country'
    }
}

i do have some more code but its irellevant logic, my tests work fine when i do not inject the $stateParmas to the controller.
so heres the Test file:

    describe('nameLocation component Controller', function () {
    var $controller,
            hotelDataServiceMock,
           stateParams,
            hotelId = "4611";
Keys = {
        Name: 'Name',
        Street: 'Street',
        City: 'City',
        State: 'State',
        Zip: 'Zip',
        Country: 'Country'
    }//@
    beforeEach(module('hc.hotelContent'));
        beforeEach(module('hc.app.core'));
        beforeEach(inject(injectFn));
       function injectFn(_$controller_, $stateParams, _hotelDataService_) {
            $controller = _$controller_;
            stateParams = $stateParams;

            hotelDataServiceMock = _hotelDataService_;
        } //controller injection fn

        function initCtrl() {
            controller = $controller('NameLocationController', {
                $stateParams: stateParams,
                hotelDataService: hotelDataServiceMock
            });
        }
describe('inserting a new hotel', function () {
        it('should populate Keys object', function () {
            var controller = initCtrl();
            expect(controller.Keys).toEqual(Keys);
        });
   }
}

im getting the Unknown Provider error. which has nothing to yet with my controller, all im doing in the controller is getting the variable set to the $stateParams variable. how do i work with this injection? my karma.conf file is configured to load jquery,angular,ui-router,mocks in this particular order and after that all the js and html

edit: i did see this post this post before but im using ui-router in my main app module, so ive added beforeEach(module('hc.app')); to the code but still nothing

Aucun commentaire:

Enregistrer un commentaire