spyOn功能不起作用 - 茉莉花

时间:2016-10-27 10:11:44

标签: javascript reactjs jasmine ecmascript-6

import * as PlanReducer from '../../../reducers/planReducer';

describe('currentPlanReducer()', () => {

   beforeAll(() => {

      spyOn(PlanReducer, 'planReducer').and.callThrough();

   });
});

PlanRedcuer是:

export default function planReducer(state = INITIAL_STATE, action) {....}

它在测试期间抛出一个错误,计划redRedcuer不是一个方法,我试图导入其他类似的方法(这不是导出默认值)并且它有效。任何人都可以帮助我如何监视这个功能并进行测试吗?

2 个答案:

答案 0 :(得分:1)

您未将planReducer功能导出为planReducer,而是default。因此,在测试中你应该监视default属性:

spyOn(PlanReducer, 'default').and.callThrough();

或者,您可以将planReducer导出为defaultplanReducer

const planReducer = function planReducer(state = INITIAL_STATE, action) {...}
export {planReducer}
export default planReducer

答案 1 :(得分:0)

试试这个:import PlanReducer from '../../../reducers/planReducer';

如果您在planReducer中查看生成的javascript,您会看到'export default function planReducer'结果为“exports.default = planReducer;'

在测试/规范import * as PlanReducer from '../../../reducers/planReducer';生成的javascript中导致spyOn(planReducer, 'planReducer').and.callThrough();

但使用import PlanReducer from '../../../reducers/planReducer';会导致spyOn(app_1.default, 'planReducer').and.callThrough();引用默认导出。