酶测试,不确定



我正在尝试测试此组件方法:

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;
    });
  });
});

如何使该信息可用于测试?

对象customer定义到底在哪里?如果是全球,只需在您的测试中在全球定义它:

global.customer = {};

否则,通常总是通过props传递组件中使用的数据。因此,当您使用组件时,只需将其传递:

<Component customerId={customer.id} />

然后测试不那么痛苦。

相关内容

最新更新