BonJon
BonJon

Reputation: 799

How to test rootscope in my angular app

I am having an issue on testing the rootscope in my case

I have something like

describe('test controller', function () {
    var myCtrl, scope, rootScope;

    beforeEach(module('myApp'));
    beforeEach(inject(function (_$controller_, _$httpBackend_, _$rootScope_) {
        rootScope   = _$rootScope_;
        scope        = _$rootScope_.$new();

        rootScope = {item:{condition:'new'}};

        myCtrl = _$controller_('myCtrl', {
            $scope: scope
        });
    }));

    describe('test', function() {
        it('should test rootscope' , function() {
            scope.setCondition('old');
        });
    });
});

html

   $scope.setCondition = function(con) {
            $scope.condition  = con;
            setRoot(con);
    };

    var setRoot = function(con) {
        $rootScope.item.condition = con;
    }

I am getting 'undefined' is not an object (evaluating '$rootScope.item.condition=con')

I am not sure what went wrong as I specify my rootscope inside beforeEach. Can anyone help me about it? Thanks a lot!

Upvotes: 1

Views: 62

Answers (1)

Phil
Phil

Reputation: 164768

This line

rootScope = {item:{condition:'new'}};

is your problem. You're overwriting your local rootScope variable and not doing anything to $rootScope.

Try this instead

rootScope.item = {
    condition: 'new'
};

Upvotes: 2

Related Questions