sinon not stubbing property value

Westy picture Westy · Nov 22, 2017 · Viewed 11.7k times · Source

I am using sinon v4.1.2. According to the documentation (http://sinonjs.org/releases/v4.1.2/sandbox/), I should be able to set a property using the following:

sandbox.stub(myObject, 'hello').value('Sinon');

However, I am getting the error:

Property 'value' does not exist on type 'SinonStub'

What is the real way to do this? I looked through all the available functions, and tried returnValue, but that isn't a valid function either.

The following was working on an older version of sinon:

sandbox.stub(myObject, 'hello', 'Sinon');

Answer

Jonathan Benn picture Jonathan Benn · Nov 23, 2017

This works for me with Sinon.JS v4.1.2:

myObject = {hello: 'hello'}
sandbox = sinon.createSandbox()
sandbox.stub(myObject, 'hello').value('Sinon')
myObject.hello // "Sinon"
sandbox.restore()
myObject.hello // "hello"