Implement $ stateParams to test angular testing with karma

Let's 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 have some more code, but its irellevant logic, my tests work fine when I do not insert $ stateParmas into the controller.
, so heres a 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 gets an unknown vendor error. which has nothing to do with my controller, all im running in the controller get 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 js and html

edit: , im ui-router , ive beforeEach (module ('hc.app ")); ,

+4
2

$stateParams :

$state,

: $state.go('namedurl', {your_params_here}) $rootScope.$digest() $

$stateParams. $stateParams , $state.

+5

, injectFn $stateParams , (_$stateParams_)? _$stateParams_ undefined injectFn.

, ui-router , ? .

, $stateParams.hotelId, $stateParams :

function injectFn(_$controller_, _hotelDataService_, $provide) {
    $controller = _$controller_;
    hotelDataServiceMock = _hotelDataService_;
    $provide.value('$stateParams', {
        hotelId: 'someId',
    });
}

function initCtrl() {
    return $controller('NameLocationController', {
         hotelDataService: hotelDataServiceMock,
    });
}
0

Source: https://habr.com/ru/post/1622624/


All Articles