【问题标题】:Stubbing a get method using Sinon使用 Sinon 存根 get 方法
【发布时间】:2015-04-18 15:33:34
【问题描述】:

我正在尝试使用属性对对象的 get 方法进行存根,

工作正常:

sinon.stub(input.model, 'get');
input.model.get.returns(10);

但考虑我们是否需要对对象中的某些特定属性进行存根,

例如:

input.model.get('yourValue') 

↪ 这怎么能被存根?有什么想法吗?

【问题讨论】:

    标签: javascript unit-testing sinon


    【解决方案1】:

    stub.withArgs() 应该做你想做的事。见http://sinonjs.org/docs/#stubs。

    sinon.stub(input.model, 'get').withArgs('yourValue').returns(10);
    

    诗乃自从changed这个语法:

    class Foo {
      get bar() { 
        return 'yolo'; 
      }
    }
    
    const myObj = new Foo();
    
    sinon.stub(myObj, 'bar').get(() => 'swaggins');
    
    myObj.bar; // 'swaggins'
    

    【讨论】:

    • 现在,你也可以sinon.stub(myObj, 'bar').value('swaggins')。
    猜你喜欢
    • 2021-06-10
    • 1970-01-01
    • 2014-07-17
    • 2014-08-08
    • 2017-07-09
    • 2017-04-27
    • 2016-01-02
    • 2021-04-10
    • 2018-06-20
    相关资源
    最近更新 更多