【发布时间】:2022-01-06 23:26:55
【问题描述】:
所以我正在为我的一个反应项目编写测试,我只是决定使用模拟服务工作者来模拟我的 api 调用,我正在尝试模拟登录端点。所以我试图在我返回的地方模拟登录错误当输入与特定电子邮件不匹配时出现错误消息。给定下面的代码;
const server = setupServer(
rest.post("https://testlogin.com/api/v1/login", (req, res, ctx) => {
// the issue is getting the email from the request body something like the code just below
if (req.body["email"] != "test@example.com") {
ctx.status(401);
return res(
ctx.json({
success: false
})
);
}
})
);
我该怎么做?有没有更好的方法来做到这一点?
【问题讨论】: