簡體   English   中英

使用Sinon對get方法進行存根

[英]Stubbing a get method using Sinon

我正在嘗試使用屬性存根對象的get方法,

工作良好:

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

但考慮一下我們是否需要在對象中存根某些特定屬性,

例如:

input.model.get('yourValue') 

↪這怎么可以被打斷? 任何的想法?

stub.withArgs()應該做你想要的。 http://sinonjs.org/docs/#stubs

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

Sinon后來改變了這種語法:

class Foo {
  get bar() { 
    return 'yolo'; 
  }
}

const myObj = new Foo();

sinon.stub(myObj, 'bar').get(() => 'swaggins');

myObj.bar; // 'swaggins'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM