How to mock several gets in fetch-mock?

倾然丶 夕夏残阳落幕 提交于 2019-12-10 18:05:00

问题


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

    //...
}

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.

How can I do this?


回答1:


Use overwriteRoutes option

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

    //...
}


来源:https://stackoverflow.com/questions/49031208/how-to-mock-several-gets-in-fetch-mock

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!