在ReactJS中模拟axios中url中的数字

问题描述 投票:0回答:1

我正在从事React项目。我想嘲笑服务电话。我想模拟数字部分。我想使用Axios模拟服务调用。谁能帮我解决这个问题?

这就是我嘲笑服务呼叫的方式。服务为/ abc / 101 / efg / 341时,它可以工作。

mock.onGet('/abc/101/efg/341').reply(200, hij);

我希望该格式的所有模拟值的服务调用都能正常工作:/ abc / {任何数字} / efg / {任何数字}。

我不知道要使用哪种方式的排卵?

reactjs nsregularexpression
1个回答
0
投票

为什么不只使用模板字符串而不是正则表达式?

mock.onGet(`/abc/${Math.floor(Math.random()*1000)}/efg/${Math.floor(Math.random()*1000)}`).reply(200, hij); 

0
投票

我喜欢使用moxios https://github.com/axios/moxios,您可以使用正则表达式作为测试的URL

moxios.stubRequest("/abc/[0-9]+/efg/[0-9]+")

希望它可以帮助您

© www.soinside.com 2019 - 2024. All rights reserved.