【发布时间】:2020-03-20 23:49:16
【问题描述】:
我遇到了将类作为参数传递的模拟函数的问题。由于某种原因,它似乎没有正确断言内容
// ErrorHandler.js
class ErrorHandler extends Error {
constructor(status, reason) {
super();
this.status = status;
this.reason = reason;
}
}
export {
ErrorHandler
}
// express file express.js
const method = async (req, res, next) => {
try {
throw new ErrorHandler(401, 'error')
next()
} catch (error) {
next(error)
}
}
// test file
it('should call the next method with the proper error', async () => {
const request = {
body: {}
}
const next = jest.fn()
const response = mockResponse() // here it's just a simple mock
await method(request, response, next)
expect(next).toHaveBeenCalledWith(
// here the problem is that it doesn't seem to assert the parameters
// and this test is passing
new ErrorHandler('random text')
)
})
我尝试模拟 ErrorHandler 类,但是它给出了另一个错误,它无法再比较下一个方法
【问题讨论】:
-
postLogin和method一样吗? toHaveBeenCalledTimes 也需要一个数字。请给一个正确的minimal reproducible example。
-
@jonrsharpe 是的,确实是一样的,我也把它改成了“With”