【问题标题】:How to mock several gets in fetch-mock?如何在 fetch-mock 中模拟几个获取?
【发布时间】:2018-02-28 13:54:31
【问题描述】:

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

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

    //...
}

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

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

我该怎么做?

【问题讨论】:

    标签: unit-testing mocking fetch fetch-mock


    【解决方案1】:

    使用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@9.5.0),我必须在每个模拟选项中包含 { repeat: 1 } - 否则它会继续给我第一个结果。
    猜你喜欢
    • 2022-06-23
    • 1970-01-01
    • 2012-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多