【问题标题】:Jasmine tohaveBeenCalledWith() Object comparisonJasmine tohaveBeenCalledWith() 对象对比
【发布时间】:2021-05-19 17:19:07
【问题描述】:

我的 jasmine 测试目前在 expect(mySpy).toHaveBeenCalledWith(myArray) 上对我来说失败,并显示以下消息,我不确定如何解释:

预期的间谍 mySpy 已使用 [ Object({ type: Function, name: 'foo', value: [[ Object({type: Function, name: 'one' value: '1' }), Object({type: Function, name: 'two', value: '2'}), ... ]] }) ]

但实际调用是 [ Object({ type: Function, name: 'foo', value: [[ 对象,对象,... ]] }) ]

请注意,预期的对象具有 value 属性的完整类型和值,但在实际调用中,Jasmine 只发现它们是对象,没有关于它们内部值的详细信息。我知道 jasmine 很难比较函数,您需要传递对两者的相同引用,我就是这样,但为什么 Jasmine 没有提供实际调用的全部细节,以及为什么仅仅因为它们是对象而失败?

【问题讨论】:

    标签: javascript jasmine


    【解决方案1】:

    这很奇怪,我是这样处理这个问题的:

    // get a handle on the arguments that the most recent call the spy was invoked with
    const arguments = mySpy.calls.mostRecent().args;
    console.log({ arguments });
    expect(arguments[0]).toBe(....); // assert the first argument to be of ...
    

    【讨论】:

    • 谢谢,这似乎是我的测试的一种解决方法,.toEqual() 检查似乎对这个论点很有效。仍然不确定为什么 .toHaveBeenCalledWith() 会以不同的方式失败
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-07
    • 2014-05-20
    • 2014-02-01
    • 1970-01-01
    • 2012-11-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多