【问题标题】:How do I refactor the jasmine.any() functionality using Chai / Sinon如何使用 Chai / Sinon 重构 jasmine.any() 功能
【发布时间】:2015-12-10 11:51:00
【问题描述】:

我在 Jasmine 中有以下代码,其中 add 是间谍。

expect(add).toHaveBeenCalledWith('MY_OBJECT_ID', jasmine.any(Object));

我将如何使用 Chai / Sinon 来表达这一点?我知道 Sinon 会使用 to.have.been.call.with() 但我遇到问题的部分是 jasmine.any() 函数。

【问题讨论】:

    标签: javascript jasmine bdd sinon chai


    【解决方案1】:

    我做了一些挖掘并想出了这个....

    expect( add.lastCall.args[0] ).to.equal('MY_OBJECT_ID');
    expect( add.lastCall.args[1] ).to.be.an('object');
    

    【讨论】:

    • 第二行非常适合我的问题。 .to.be.an('object');取代 jasmine.any(Object)
    【解决方案2】:

    诗乃有calledWithMatch():

    var sinon = require('sinon');
    var spy   = sinon.spy();
    
    spy('MY_OBJECT_ID', { foo : 'bar' });
    
    console.log(spy.calledWithMatch('MY_OBJECT_ID',     sinon.match.object) ); // true
    console.log(spy.calledWithMatch('NOT_MY_OBJECT_ID', sinon.match.object) ); // false
    console.log(spy.calledWithMatch('MY_OBJECT_ID',     sinon.match.number) ); // false
    

    【讨论】:

      猜你喜欢
      • 2017-06-29
      • 2017-10-08
      • 1970-01-01
      • 1970-01-01
      • 2020-07-11
      • 2017-10-23
      • 1970-01-01
      • 2020-08-18
      • 1970-01-01
      相关资源
      最近更新 更多