我试图用它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
. 我究竟做错了什么?