本文介绍了如何模拟几个获取模拟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在测试我的react组件,并且我想模拟几个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 });
//...
}
这篇关于如何模拟几个获取模拟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!