意外的酶测试输出

时间:2018-12-04 09:30:02

标签: javascript reactjs unit-testing enzyme

我是作为Enzyme的ReactJS项目测试包的新手。我已经为类组件编写了该测试:

test.js:

import expect from 'expect';
import React from 'react';
import sinon from 'sinon'
import {mount, shallow} from 'enzyme';
import TestUtils from 'react-addons-test-utils';
import PageToBeTested from './PageToBeTested';

describe('Testing PageToBeTested via Enzyme', () => {
  it('onChangeMethod is called when a field is updated', () => {
    const event = {value: {"siteCode": 123}}
    const wrapper = mount(<PageToBeTested />);
    const handleOnChangeMethodSpy = sinon.spy(wrapper.instance(), 
"onChangeMethod ");
    wrapper.update(); // Force re-render
    wrapper.find('SelectInput').at(0).simulate("change", event);
    expect(handleOnChangeMethodSpy.calledOnce).toEqual(true);
  });
});

我收到以下消息:

Testing PageToBeTested via Enzyme
       onChangeMethod is called when a field is updated:

  Error: Expected false to equal true
  + expected - actual

  -false
  +true

这是我的PageToBeTested的样子:

// Shortened for simplicity
onChangeMethod(event) {
    const region = event.value;
    this.setState({
      region: region
    });
    methodA(region).then(response => {
      this.setState({
        someList: methodB(response.data.sites) 
      });
    });
  }

  return (
    <Container fluid>
      <Row>
        <ConnectedComponent
          onChangeMethod={this.onChangeMethod} />
      </Row>
      {ChildComponent}
    </Container>
  );

我遵循了这个stackoverflow答案here,我希望调用onChangeMethod,但是从错误看来它根本没有被激发。

“ SelectInput”是“ ConnectedComponent”的子组件。

在这种情况下,我可以知道幕后发生了什么吗?

谢谢。

0 个答案:

没有答案