我想模拟路线路径,例如[http://localhost:3000/bookings/:bookingid]用于在app.test.js文件中进行测试。如何实现?
答案 0 :(得分:0)
您可以通过测试route函数的内容并模拟自己的Request对象来测试路由。
这是一个如何模拟测试路线的示例:
app.js
router.post("/bookings/:bookingid", (req, res) => {
// validate credentials
const error = validateCredentials(req.body.username, req.body.password, req.params.bookingid);
...
}
app.test.js
describe("Booking routes", function() {
const username = "John";
const password = "stackoverflow";
const bookingid = `123`;
it("gets booking id", function() {
expect(validateCredentials(username, password, bookingid)).to.be.undefined;
});
});