mocha/chai 如何测试方法在 then 或 catch 回调中被调用

mocha/chai how to test that a method is called inside a then or catch callback

我正在尝试测试这样的东西:

// component.js
import React from 'react';
import AsyncModule from '../some/async/module';
import { doSomething } from '../myModule';

.
.
.

const onSubmit = () => {
  const opts = {/* some config */};

  AsyncModule(opts).someAsyncCall().then(resolved => {
    doSomething();
  }).catch(e => {
    // also
    doSomething();
  });
}

class MyCompontent extends React.Component {

  render() {
    return <Formik onSubmit={onSubmit} ... />;
  }

}

我需要测试是否正在调用 doSomething。到目前为止,我已经编写了一些测试,并且我已经尝试了一些东西但是 none 已经奏效了。我需要你的帮助:

.
.
.
  it.only('should doSomething on Formik submit when AsyncModule.someAsyncCall promise resolves', () => {
    spyDoSomething = spy(myModule, 'doSomething');
    const expectedResponse = {};
    const stubSomeAsyncCall = stub(AsyncModule, 'someAsyncCall');
    stubSomeAsyncCall.resolves(expectedResponse);
    wrapper = mount(<MyCompontent />);
    const formik = wrapper.find('Formik');

    formik.simulate('submit');

    return expect(spyDoSomething).to.has.been.called;
  });

这个测试显然没有通过。如何检查 doSomething 是否在 thencatch 回调的正文中被调用?

Im 运行 node 9.1.0、mocha、chai、enzyme 和 sinon。

使用笑话

import AsyncModule from '../some/async/module';
import { doSomething } from '../myModule';

jest.mock('../myModule');
jest.mock('../some/async/module', () => ({
  someAsyncCall: jest.fn()
}));

it('should doSomething on Formik submit when AsyncModule.someAsyncCall promise resolves', () => {
    const expectedResponse = {};

    AsyncModule.stubSomeAsyncCall.mockReturnValue(expectedResponse);
    wrapper = mount(<MyCompontent />);
    const formik = wrapper.find('Formik');

    formik.simulate('submit');

    expect(doSomething).to.has.been.called;
  });


使用proxyquire

it('should doSomething on Formik submit when AsyncModule.someAsyncCall promise resolves', () => {
    const expectedResponse = {};
    const spyDoSomething = sinon.spy();

    proxyquire('../myModule', {
      doSomething: spyDoSomething
    });
    proxyquire('../some/async/module', {
      someAsyncCall: sinon.stub().returns(expectedResponse)
    });


    wrapper = mount(<MyCompontent />);
    const formik = wrapper.find('Formik');

    formik.simulate('submit');

    return expect(spyDoSomething).to.has.been.called;
  });