【发布时间】:2020-11-13 21:50:57
【问题描述】:
我正在学习做 api rest 测试,我在使用 chai 和 mocha 时遇到了问题,我关注了 this example。
问题是 POST 方法总是挂起的,根据挂起的东西并不意味着它失败了。但我想通过这个测试。
我的路线返回了 json 的创建内容,所以我不明白为什么测试没有通过,有人可以帮我吗?
如果有帮助,请链接到 repository。
POST 路由代码
router.post("/game", async (req, res) => {
const title = req.body.title
const year = req.body.year
const price = req.body.price
try {
const gameCreated = await Game.create({
title: title,
year: year,
price: price
})
res.json(gameCreated)
} catch (err) {
console.log(err)
res.sendStatus(500)
}
})
测试代码
describe("POST game test", () => {
it("must create a new game"), (done) => {
let game = {
title: "Game created by mocha",
year: 2020,
price: 178
}
chai.request('localhost:3033')
.post('/game')
.send(game)
.end((err, res) => {
res.should.have.status(200)
done()
})
}
})
【问题讨论】:
标签: node.js rest mocha.js integration-testing chai