import * as PlanReducer from '../../../reducers/planReducer';
describe('currentPlanReducer()', () => {
beforeAll(() => {
spyOn(PlanReducer, 'planReducer').and.callThrough();
});
});
PlanRedcuer是:
export default function planReducer(state = INITIAL_STATE, action) {....}
它在测试期间抛出一个错误,计划redRedcuer不是一个方法,我试图导入其他类似的方法(这不是导出默认值)并且它有效。任何人都可以帮助我如何监视这个功能并进行测试吗?
答案 0 :(得分:1)
您未将planReducer
功能导出为planReducer
,而是default
。因此,在测试中你应该监视default
属性:
spyOn(PlanReducer, 'default').and.callThrough();
或者,您可以将planReducer
导出为default
和planReducer
:
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();
引用默认导出。