【问题标题】:how can I assert boolean in Jasmine using "toHaveBeenCalledWith"如何使用“toHaveBeenCalledWith”在 Jasmine 中断言布尔值
【发布时间】:2020-07-23 15:35:03
【问题描述】:

我试图断言一个函数被调用:

expect(functionA).toHaveBeenCalledWith(xxx, yyyy, true);

我不关心xxx和yyyy是什么,我只关心最后一个参数是否为真。

我如何在 Jasmine 中做到这一点?我试过了:

expect(functionA).toHaveBeenCalledWith(jasmine.objectContaining(true));

虽然 true 不是对象。

我也试过了:

expect(functionA.calls.mostRecent().args[2]).toMatch(true);

但它给了我错误:

Error:<toMatch>:Expected is not a String or a RegExp

我应该如何断言它?我是 Jasmine 的新手,谢谢!

【问题讨论】:

    标签: javascript testing jasmine


    【解决方案1】:

    假设 xxx 和 yyyy 是字符串,你可以这样做:

    expect(functionA).toHaveBeenCalledWith(jasmine.any(String), jasmine.any(String), true);
    

    另见文档:official docs

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-21
      • 2014-02-09
      • 1970-01-01
      • 2014-02-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-15
      • 2021-06-22
      相关资源
      最近更新 更多