【发布时间】:2020-07-20 15:43:09
【问题描述】:
我正在尝试了解 tdd。我已经写了这个测试
it("should call TodoModel.findById", async () =>{
await TodoController.getTodoById(req,res,next)
req.params.todoId = "5f1216dd46a9c73dd812be36"
expect(TodoModel.findById).toBeCalledWith("5f1216dd46a9c73dd812be36");
})
对于下面的函数
exports.getTodoById = async (res, req, next) => {
const todoById = await TodoModel.findById(req.params.todoId)
}
但我不断得到
● TodoController.getTodoById › 应该调用 TodoModel.findById
TypeError: Cannot read property 'todoId' of undefined
22 |
23 | exports.getTodoById = async (res, req, next) => {
> 24 | const todoById = await TodoModel.findById(req.params.todoId)
| ^
25 | }
at Object.getTodoById (controllers/todo.controller.js:24:57)
at Object.<anonymous> (tests/unit/todo.controller.test.js:54:30)
为什么会这样?我在测试中使用预制 ID 定义它。它应该被定义为afaik
【问题讨论】:
-
我可以看看你的路线吗?
-
@Shubh 我还没有定义它。但添加
router.get("/:todoId", todoController.getTodoById)并不能解决问题
标签: javascript node.js express jestjs web-api-testing