【发布时间】:2021-11-16 16:15:32
【问题描述】:
这在文档中并不明显,但是...toBeCalledWith 和 toHaveBeenCalledWith 之间有什么区别?
【问题讨论】:
标签: jestjs
这在文档中并不明显,但是...toBeCalledWith 和 toHaveBeenCalledWith 之间有什么区别?
【问题讨论】:
标签: jestjs
来自文档https://jestjs.io/docs/en/expect#tohavebeencalledwitharg1-arg2-
.toHaveBeenCalledWith(arg1, arg2, ...)下的别名:.toBeCalledWith()
来自源代码:
//...
toBeCalledWith: createToBeCalledWithMatcher('toBeCalledWith'),
toHaveBeenCalled: createToBeCalledMatcher('toHaveBeenCalled'),
toHaveBeenCalledTimes: createToBeCalledTimesMatcher('toHaveBeenCalledTimes'),
toHaveBeenCalledWith: createToBeCalledWithMatcher('toHaveBeenCalledWith'),
//...
它们是由 createToBeCalledWithMatcher 函数创建的,只是名称不同。
所以,它们是一样的。
更新:这是我个人对 jestjs 为什么提供这些匹配器 API 别名的理解。
jestjs 基于 jasmine 测试运行器构建,请参阅 Jasmine and Test Assertion Improvements
jasmine 只提供一个匹配器 - toHaveBeenCalledWith。
jestjs 提供了比jasmine 更好的匹配器API,toBeCalledWith 别名更短、更容易记住、更容易使用。 "have been"
【讨论】: