Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stubbing a get method using Sinon

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?

like image 457
Sai Avatar asked Feb 17 '15 19:02

Sai


1 Answers

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'
like image 104
dawner Avatar answered Sep 20 '22 16:09

dawner