user3701057
user3701057

Reputation: 395

karma + jasmine + angular + ui router - unit test for a state with parent property

I have an angular module, lets say, moduleA. I am using angular ui-router to define the states.

moduleA has state stateA with resolve.

$stateProvider
                .state('stateA', {
                    abstract: true,
                    parent: 'secure',
                    templateUrl: baseUrl + '/templates/root.html',
                    controller: 'rootCtrl',
                    resolve: {
                        value1: [ 'StateService', function ( StateService) {
                            return StateService.getValue1();
                        }],

                        value2: ['Service2', function(Service2) {                               
                                return Service2.getValue2();
                            }]
                    }
                })

Then, I have another module "moduleB" in which I define state stateB with parent property stateA.

$stateProvider
                .state('stateB', {
                    parent: 'stateA',
                    abstract: true,
                    templateUrl : baseUrl+'/templates/stateB.html'
                })
                .state('stateB.details', {
                    url: '/stateB/details/:param1/:param2',
                    resolve : {
                        value3 : ['$localStorage', '$stateParams', function($localStorage, $stateParams){
                            return $localStorage.value3[$stateParams.param1];
                        }]
                    },
                    views : {
                        'view1' : {
                            templateUrl : baseUrl+'/templates/view1.html',
                            controller: 'View1Ctrl'
                        },
                        'view2' : {
                            templateUrl : baseUrl+'/templates/view2.html',
                            controller : 'View2Ctrl'
                        }
                    }
                })

In my Karma - Jasmine unit test for moduleB, I have

beforeEach(inject(function($rootScope, $state, $injector, $httpBackend) {
            rootScope = $rootScope;
            state = $state;
            injector = $injector;
            httpBackend = $httpBackend;

        }));


        it('should be an abstract state', function(){
            var s = state.get('stateB');
            expect(s.abstract).toBeTruthy();
        });

Test fails with error TypeError: 'null' is not an object (evaluating 's.abstract')

If I comment the line parent: 'stateA', from stateB's definition, test will pass with success.

Can anyone help me?

Upvotes: 0

Views: 1069

Answers (1)

user3701057
user3701057

Reputation: 395

Fixed. I was loading "moduleB" in my tests but not moduleA. I fixed my issue by loading "moduleA" in my tests. I had to add beforeEach(module('moduleA'));

Upvotes: 3

Related Questions