【问题标题】:integration tests - redux/react + nock.js集成测试 - redux/react + nock.js
【发布时间】:2018-10-15 14:15:06
【问题描述】:

我不知道如何找到编写此集成测试的方法。 我使用酶来模拟反应组件,玩笑来测试和 nock 来模拟 axios api 调用。

到目前为止,我创建了模拟点击按钮的测试,我想模拟 api 调用。

在互联网上没有太多帮助。

我的测试:

 it('Should invoke clear action and clear the group', (done) => {
    // GIVEN
    const clearButtonComponent = wrapper.find('[id="123"]');
    nock('http://localhost:8080')
      .intercept('/path/api/brum/123/group', 'DELETE')
      .reply(200, {
        status: 200,
        message: 'cleared',
      });
    const service = new myService();

    // WHEN
    clearButtonComponent.first().simulate('click');
    const result = Promise.resolve(service.clearGroup(123));

    // THEN
    expect(result).toEqual({ x: 'x' }); // I know it's not what I expect
    wrapper.update();
    done(); 
  });

异步操作还原:

export const clearGroup = id=> (dispatch, getState) => {
  myService.clearGroup(id)
    .then(() => {
        return dispatch(getGroup(id))
    });
};

myService 中的方法:

  clearGroup(id) {
    return this._delete(`/${id}/group`);
  }

当然路径更复杂,但我的服务扩展了具有此基本 url 的基本服务。

谁能告诉我如何模拟它以让代码更进一步?

它仍然抱怨 id 未定义 - 看起来 nock 没有模拟它。

【问题讨论】:

    标签: reactjs testing redux nock


    【解决方案1】:

    我会放弃 nock(这些天我尝试只用它来测试客户)并开玩笑地模拟 myService

    我不使用 axios,所以没用过这个,但它可能会成功..https://github.com/knee-cola/jest-mock-axios.

    否则你可以考虑编写自己的模拟......https://jestjs.io/docs/en/es6-class-mocks

    【讨论】:

    • lector,感谢您的回答。问题是我无法安装任何不同的依赖项。那么解决方案可以是对我的服务进行模拟,然后对其进行模拟吗? :) 问候
    • 是的,应该不会太难,虽然我们看不到足够的 myService 来确定。
    猜你喜欢
    • 1970-01-01
    • 2016-12-24
    • 1970-01-01
    • 2021-05-27
    • 1970-01-01
    • 2020-05-17
    • 2019-03-22
    • 1970-01-01
    • 2021-10-26
    相关资源
    最近更新 更多