简体   繁体   English

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

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

Im trying to test something like this:我试图测试这样的事情:

// 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} ... />;
  }

}

I need to test that doSomething is being called.我需要测试正在调用doSomething So far, I have some tests written, and i've tried some things but none have worked.到目前为止,我已经编写了一些测试,并且尝试了一些东西,但都没有奏效。 I need your help with this:我需要你的帮助:

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

this test obviously doesn't pass.这个测试显然没有通过。 How can I check that doSomething is being called in the body of the then and catch callbacks?如何检查then的主体中是否正在调用doSomethingcatch回调?

Im running node 9.1.0, mocha, chai, enzyme and sinon.我正在运行节点 9.1.0、mocha、chai、酶和 sinon。

Using jest使用玩笑

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


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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 异步测试-Mocha和Chai-确保正在调用done()回调 - Async Tests - Mocha and Chai - Ensure the done() callback is being called 如何使用React-Redux中的Mocha,Chai和Enzyme测试方法和回调 - How to test methods and callback using Mocha, Chai, & Enzyme in React-Redux Sinon:测试React组件方法正在调用函数传递作为道具(Mocha + Chai + Sinon正在用于单元测试) - Sinon: Test React component method is calling function pass as a prop (Mocha + Chai + Sinon is being used for unit test) 如何测试是否在componentDidMount中调用了方法? - How to test if method was called inside componentDidMount? 流星和摩卡柴:测试插入功能 - Meteor & Mocha Chai : test insert function 如何使用酶,摩卡,柴对单元自定义的React函数进行单元测试 - How do you unit test a custom React function using enzyme, mocha, chai 你如何测试酶/摩卡/柴中div或其他元素的含量? - How do you test the content of a div or other element in enzyme/mocha/chai? 如果变量具有反应组件作为值,我如何使用 mocha/chai/sinon 进行单元测试? - How do i unit test using mocha/chai/sinon if a variable has a react component as value? 如何使用业力摩卡/柴酶对基本三元进行单元测试? - how to unit test a basic ternary using karma-mocha/chai-enzyme? 使用 enzyme、mocha 和 chai 测试带有回调的 window 事件 addEventListener - Testing a window event addEventListener with a callback using enzyme, mocha and chai
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM