酶测试,未定义,

时间:2017-04-28 05:40:55

标签: reactjs mocha enzyme

我试图测试这个组件方法:

componentWillMount() {
    if (this.props.location.query.customerId) {
      const customer = _.find(this.props.customers.data, ['id', this.state.customerId]);
      this.props.dispatch(getTransactionReceipts(this.props.user, this.state.dateRange));
      this.props.dispatch(setVisibilityFilter({customerId: customer.id,}, 'filter'));
      this.props.dispatch(getCustomers(this.props.user)).then(() => {
        this.props.dispatch(setCustomer(customer));
      });
    }
    else {
      this.loadData(this.props);
      if (this.props.location.query.receiptId) {
        let receiptId = this.props.location.query.receiptId;
        this.props.dispatch(setVisibilityFilter(receiptId));
      }
      else {
        this.props.dispatch(setVisibilityFilter({property: 'type', value: 'All Transactions'}, 'filter'));
      }
    }
  }

但是在运行测试时出现此错误:TypeError: Cannot read property 'id' of undefined

我知道这是因为customer.id部分,但我不知道如何嘲笑它并让它可以进行测试。

以下是我所拥有的:

  beforeEach(function () {
    stubs = {};
    stubs.dispatch = sinon.stub();
    stubs.windowOpen = sinon.stub();
    stubs.handleFilterSelection = sinon.stub();
    stubs.handleVoidClose = sinon.stub();
    stubs.processCreditCardVoid = sinon.stub();

    loadDataSpy = sinon.spy(ComponentUnderTest.prototype, 'loadData');

    stubs.dispatch.returns(new Promise(function (resolve, reject) {
      resolve({});
    }));

    componentProperties = _.cloneDeep(mockComponentProperties);
    componentProperties.location = {
      query: {
        customerId: 11111
      }
    };

  });

  afterEach(function () {
    _.invokeMap(stubs, 'restore');
    loadDataSpy.restore();
    mockComponentProperties.dispatch.reset();
  });

  describe('in general', () => {

    beforeEach(function () {
      wrapper = shallow(<ComponentUnderTest {...componentProperties} />);
    });

    it('should render account activity Transactions component', function () {

      expect(wrapper.find('.activityTransactions').length).to.be.ok;

    });

  });
});

有关如何使这条信息可用于测试的任何指示?

0 个答案:

没有答案