1

我正在尝试对我的一个模块进行测试,该模块处理 ngRedux。在运行测试时,我得到

TypeError: this.ngRedux.getState 不是函数

我正在运行的测试很简单

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

包.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",

组件代码

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

1 回答 1

0

在这篇很棒的博文Testing with Mocks & Spies中找到了我的答案

更新

添加我的固定代码

 let spySomeService = {
    getData: () => {
    },
    getUserRole: () => {
    }
}

providers: [...
            {provide: SomeService, useValue: spySomeService},

 it("should create component:", () => {
    spyOn(spySomeService, 'getUserRole').and.returnValue("OK");
    expect(comp).toBeDefined();
});
于 2017-09-11T09:43:49.527 回答