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 badges1 Answer
Reset to default 20stub.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'