我正在尝试使用proxyquire
替换我正在测试的模块中的方法调用,但它正在调用方法,尽管我已经设置了存根。我做错了什么?
formsReducer.test.js:
import { expect } from 'chai';
import * as actions from '../actions/formsActions';
import * as types from '../constants/actionTypes';
import proxyquire from 'proxyquire';
describe('Forms Reducer', () => {
describe('types.UPDATE_PRODUCT', () => {
it('should get new form blueprints when the product changes', () => {
//arrange
const initialState = {
blueprints: [
{
id: 1,
categoryId: 1,
identifier: null,
name: "Investment Policy Statement",
sortGroup: 1,
wetSignRequired: false
}
]
};
const testBlueprints = [{ id: 999, categoryId: 1, name: "Form Blueprint Loaded From Product ID 1", sortGroup: 1, wetSignRequired: false }];
//use proxyquire to stub call to formsHelper.getFormsByProductId
let formsReducer = proxyquire.noCallThru().load('./formsReducer', {
formsHelper: {
getFormsByProductId: id => { return testBlueprints }
}
}).default;
const action = {
type: types.UPDATE_PRODUCT,
product: {
id: 1,
accountTypeId: 1,
officeRangeId: 1,
additionalInfo: "",
enabled: true
},
};
//act
const newState = formsReducer(initialState, action);
//assert
expect(newState.blueprints).to.be.an('array');
expect(newState.blueprints).to.equal(testBlueprints);
});
});
});
formsReducer.js:
import * as types from '../constants/actionTypes';
import objectAssign from 'object-assign';
import initialState from './initialState';
import formsHelper from '../utils/FormsHelper';
export default function formsReducer(state = initialState.forms, action) {
switch (action.type) {
case types.UPDATE_PRODUCT: {
let formBlueprints = formsHelper.getFormsByProductId(action.product.id);
formBlueprints = formsHelper.addOrRemoveMnDisclosure(formBlueprints, action.stateOfResidence.id);
return objectAssign({}, state, {blueprints: formBlueprints, instances: []});
}
}
formsHelper.getFormsByProductId
如果通过testBlueprints
正确存根,则不会返回proxyquire
。我做错了什么?
答案 0 :(得分:0)
formsReducer.js
正在导入../utils/FormsHelper
,但是proxyquire正在尝试替换formsHelper
。尝试将proxyquire更改为:
proxyquire.noCallThru().load('./formsReducer', {
'../utils/FormsHelper': {
getFormsByProductId: id => { return testBlueprints }
}
})