我使用 axios-mock-adapter 来模拟我的 API,它工作正常,但在一个模拟上它返回 404 错误,我找不到原因。
有here带测试的沙箱,您可以看到当我们运行测试时,第二次检查失败,因为 axios POST 调用尚未被模拟。我尝试删除 header 部分,但当我运行测试时沙箱刚刚崩溃。
API 模拟(测试部分):
import axios from "axios";
import MockAdapter from 'axios-mock-adapter';
import Utils from "../Utils/Utils";
// Global variable for post request with axios
global.users_post = axios.create({
baseURL: "http://localhost:5000/api/",
headers: {'Content-Type': 'application/json'}
});
/* Mockup API */
var userMock = new MockAdapter(users_post);
const user_resp_full = {
data: {
first_name: "Test",
last_name: "Test",
email: "test@gmail.com",
address: "Test",
zipcode: 1010,
city: "Test",
admin: false
}
}
const testAPI = () => {
userMock
.onPost("users", user_resp_full, Utils.getAuth())
.reply(200, {data: {status: "success"}});
}
test("something", async () => {
let tree = shallow(<UserManage type="create" uuid="" />);
testAPI();
await flushPromises();
// Some test
tree.find("#confirm-create").simulate("click");
await flushPromises();
// Error 404, mock isn't trigger
})
我已经检查过,数据是相同的,端点相同,但似乎没有正确模拟它。
axios 类中调用:
function (fields) {
users_post.post("users", fields, Utils.getAuth())
.then(resp => {
let data = resp.data;
// Do something
})
.catch(resp => {
let data = resp.response.data;
// Display error
});
}
此时,在我的 Jest 测试中,它返回 404 错误,因此它尚未模拟我的端点 API(其他工作)。
Utils.getAuth()
函数返回带有身份验证 token 的 header 。
数据发送
涉及数据发送的内容(第一个是在使用mock进行测试调用之前,第二个是在测试函数中,数据日志是发送到api的数据):
console.log src/tests/UserManage.test.js:222
POST USER 2
{"first_name":"Test","last_name":"Test","email":"test@gmail.com","address":"Test","zipcode":1010,"city":"Test","admin":false}
console.log src/Components/Users/UserManage.js:152
POST USER
console.log src/Components/Users/UserManage.js:153
{"first_name":"Test","last_name":"Test","email":"test@gmail.com","address":"Test","zipcode":1010,"city":"Test","admin":false}
更新
仅当我使用带有如下 header 的 POST
请求时才会发生此错误:
axios.post("http://localhost/api/user/update", {name: "Test"}, {headers: {"Authorization": "Bearer token")}});
我看到了on axios-mock-adapter github test page最终我们应该将 headers
放入测试中,之前不带标签:
{headers: {Autorization: "Bearer token"}}
变为 {Autorization: "Bearer token"}
但不幸的是,它的效果并不比我好。
解决方案
根据Matt Carlotta和他的codesandbox的回复,我修改mine包含 2 个已解决问题的示例:
- 使用 axios* 测试
POST
请求模拟 - 使用 axios* 实例对
POST
请求模拟进行测试
* 使用axios-mock-adapter
最佳答案
好的,第二轮。
- 当
promise
需要一些时间响应时,您的flushPromises
函数无法正确解析promise
。解决方法是在.test.js
文件中返回
promise
并在其前面添加一个await
。由于我们在promise
上使用await
,因此不需要awaitlushPromises()
。 - 此外,在
onPost
模拟函数中包含headers
将导致函数抛出错误。由于您只是模拟此请求(而不是实际测试其集成),因此您不需要包含它们。但是,由于您已经在使用自定义axios
配置,因此您只需将headers
包含在axiosConfig.js
文件中即可。有关详细信息,请参阅您的codesandbox 的工作示例。
如下面的单元测试
代码和框中所示,如果您尝试在deleteUserDataOverTime
方法上使用awaitlushPromises()
,则会失败。它失败是因为它没有解决promise
。此 promise
需要一些时间才能解决,并且未得到正确处理。
此外,由于测试的异步
性质,您不应在同一测试文件中包含单元
和集成
测试。由于测试是异步
,因此在同一模拟请求mockAxios.reset()或mockAxios.restore()
> 或相同的模拟实例——进行任何额外的真实或虚假的 API 调用——可能并且将会无意中影响所有 API 调用(同样,它们是异步测试,而不是同步测试)。
API 单元测试的工作示例:https://codesandbox.io/s/6z36z6pzyr (假 API - 包括 GET
、PUT
、POST
和 DELETE
)
集成测试 API 的工作示例:https://codesandbox.io/s/7z93xnm206 (真正的 API - 仅包括 GET
,但 PUT
、POST
和 DELETE
的功能应保持相同)
代码和框的工作示例:https://codesandbox.io/s/526pj28n1n
关于reactjs - 为什么我的 api 模拟会返回 404 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55355254/