Search code examples
angularunit-testingkarma-jasminekarma-webpack

Karma-Jasmine TypeError: this.ngRedux.getState is not a function


I'm trying to run a test on one of my modules, which handles ngRedux. while running the test, i get

TypeError: this.ngRedux.getState is not a function

The test im running is a simple

it("should create component:", () => {
    expect(comp).toBeDefined();
});

package.json

"redux": "^3.6.0",
"redux-logger": "^2.6.1",
"redux-observable": "^0.12.2",
"karma": "^1.7.1",
"karma-chrome-launcher": "^2.2.0",
"karma-coverage": "^1.1.1",
"karma-jasmine": "^1.1.0",
"karma-mocha-reporter": "^2.2.4",
"karma-sourcemap-loader": "^0.3.7",
"karma-webpack": "^2.0.4",
"@angular/core": "4.3.6",

component code

 ...
let roleKinds = _.get(this.ngRedux.getState(), 'auth.user.Role', [])
        .filter(roleName => roles.indexOf(roleName) != -1);
...

Solution

  • found my answer in this great post Testing with Mocks & Spies

    UPDATE

    Adding my fixed code

     let spySomeService = {
        getData: () => {
        },
        getUserRole: () => {
        }
    }
    
    providers: [...
                {provide: SomeService, useValue: spySomeService},
    
     it("should create component:", () => {
        spyOn(spySomeService, 'getUserRole').and.returnValue("OK");
        expect(comp).toBeDefined();
    });