0

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

4

1 回答 1

0

formsReducer.js正在导入../utils/FormsHelper,但 proxyquire 正在尝试替换formsHelper。尝试将 proxyquire 更改为:

proxyquire.noCallThru().load('./formsReducer', {
  '../utils/FormsHelper': {
    getFormsByProductId: id => { return testBlueprints }
  }
})
于 2016-09-20T07:15:53.210 回答