最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Stubbing a get method using Sinon - Stack Overflow

programmeradmin1浏览0评论

I'm trying to stub the get method of an object with properties,

Works fine:

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

but consider if we need to stub some specific property in the object,

eg:

input.model.get('yourValue') 

↪ how this can be stubbed? Any idea?

I'm trying to stub the get method of an object with properties,

Works fine:

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

but consider if we need to stub some specific property in the object,

eg:

input.model.get('yourValue') 

↪ how this can be stubbed? Any idea?

Share Improve this question edited Jan 24, 2017 at 5:38 Abdennour TOUMI 93.2k42 gold badges267 silver badges269 bronze badges asked Feb 17, 2015 at 19:52 SaiSai 2,0326 gold badges33 silver badges56 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 20

stub.withArgs() should do what you want. See http://sinonjs.org/docs/#stubs.

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

Sinon has since changed this syntax:

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

const myObj = new Foo();

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

myObj.bar; // 'swaggins'
发布评论

评论列表(0)

  1. 暂无评论