SImples Karma test returns unsuccessful with localStorage on controller

0

Good afternoon,

I am trying to run a simple test with test runner Karma . I'm using AngularJS for this little test.

Controller ( test.controller.js ):

class TestController{
    constructor($scope, $window, $localStorage){
    'ngInject'
        this.name = "TestController";
        $localStorage.testVariable = 'ola';
    }
}
export default TestController;

Test file ( test.spec.js ):

describe('Test', () => {
    let $rootScope, makeController, $scope, $window, $localStorage;
    beforeEach(inject((_$rootScope_, _$window_, _$localStorage_) => {
        $rootScope = _$rootScope_;
        $scope = $rootScope.$new();
        $window = _$window_;
        $localStorage = _$localStorage_;
        makeController = () => {
            return new TestController($scope, $window, $localStorage);
        };
    }));

    describe('Controller', () => {
        it('has a name property', () => {
            let controller = makeController();
            expect(controller).to.have.property('name');
        });
    });
});

After running the karma start command on the console, I get the following error:

> Test
>     Controller
>     x has a name property
>         TypeError: Cannot read property 'testVariable' of undefined´

It seems like he does not like $ localStorage. Why the reason?

This happens to all controllers that deal with $ localStorage. If you remove everything relative to $ localStorage, the test passes successfully.

It seems like there are some conflicts with $ localStorage . Can someone help me?

    
asked by anonymous 17.08.2018 / 17:41

0 answers