如何在 fetch-mock 中模拟几个获取? [英] How to mock several gets in fetch-mock?

查看:18
本文介绍了如何在 fetch-mock 中模拟几个获取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试我的反应组件,我想模拟几个 get 操作.我想做的是:

I'm testing my react components and I want to mock several get operations. What I want to do is something like:

test(`Created correctly`, async () => {
    fetchMock.get(`*`, JSON.stringify(FIRSTGETOBJ));
    fetchMock.get(`*`, JSON.stringify(SECONDGETOBJ));
    fetchMock.get(`*`, JSON.stringify(THIRDGETOBJ));

    //...
}

每个 get 的 url 都是相同的,但有效负载会发生变化.但是,使用上面的代码我会得到:

The url for each get is the same, but the payload changes. However, using the code above I will get:

Error: Adding route with same name as existing route. See `overwriteRoutes` option.

我该怎么做?

推荐答案

使用 overwriteRoutes 选项

test(`Created correctly`, async () => {
    fetchMock.get(`*`, JSON.stringify(FIRSTGETOBJ));
    fetchMock.get(`*`, JSON.stringify(SECONDGETOBJ), { overwriteRoutes: false });
    fetchMock.get(`*`, JSON.stringify(THIRDGETOBJ), { overwriteRoutes: false });

    //...
}

这篇关于如何在 fetch-mock 中模拟几个获取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆