问题描述
我正在尝试使用 supertest 测试我的用户端点:
router.post("/users",async (req,res) => {
const user = new User(req.body);
try {
await user.save();
const token = await user.generateAuthToken();
res.status(201).send({ user: user,token: token });
} catch (e) {
res.status(400).send(e);
}
});
我的 user.test.js 看起来像这样:
const request = require("supertest");
const app = require("../src/app");
test("Should signup a new user",async () => {
await request(app)
.post("/users")
.send({
name: "Andrew",email: "andrew@example.com",password: "MyPass777!",})
.expect(201);
});
但是当我删除 .expect(201) 时一切正常,你能帮我吗?
解决方法
尝试改用这个:
const {status} = await request(app)
.post("/users")
.send({
name: "Andrew",email: "andrew@example.com",password: "MyPass777!",})
expect(status).toBe(201);