【问题标题】:Jest Difference Between toBeCalledWith and toHaveBeenCalledWith开玩笑 toBeCalledWith 和 toHaveBeenCalledWith 之间的区别
【发布时间】:2021-11-16 16:15:32
【问题描述】:

这在文档中并不明显,但是...toBeCalledWith 和 toHaveBeenCalledWith 之间有什么区别?

【问题讨论】:

    标签: jestjs


    【解决方案1】:

    来自文档https://jestjs.io/docs/en/expect#tohavebeencalledwitharg1-arg2-

    .toHaveBeenCalledWith(arg1, arg2, ...)下的别名:.toBeCalledWith()

    来自源代码:

    https://github.com/facebook/jest/blob/b7cb5221bb06b6fe63c1a5e725ddbc1aaa82d306/packages/expect/src/spyMatchers.ts#L1128

    https://github.com/facebook/jest/blob/b7cb5221bb06b6fe63c1a5e725ddbc1aaa82d306/packages/expect/src/spyMatchers.ts#L1131

    //...
      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"

    似乎没有太多语义需求

    【讨论】:

      猜你喜欢
      • 2021-12-16
      • 1970-01-01
      • 2018-06-05
      • 2021-11-14
      • 2019-11-08
      • 2020-07-04
      • 2021-12-30
      • 2019-06-11
      • 2017-07-26
      相关资源
      最近更新 更多