19

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');
Rubens Mariuzzo
  • 28,358
  • 27
  • 121
  • 148
Westy
  • 707
  • 2
  • 10
  • 23

2 Answers2

34

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"
Rubens Mariuzzo
  • 28,358
  • 27
  • 121
  • 148
Jonathan Benn
  • 2,908
  • 4
  • 24
  • 28
  • 1
    I am not sure what happened before, but now it is working just as it's supposed to. I thought I ran `npm install`, but maybe that didn't work the first time. Thanks for verifying that it works as documented. – Westy Nov 27 '17 at 23:14
  • `stub.value` was added in Sinon v2, FWIW. – Tgr Sep 04 '22 at 05:53
-1

In my expirience, it's not necessary to create a sandbox everytime. You can use stubs without it to reduce complexity in your code. Just define a stub like this:

const stubHello = sinon.stub(myObject, 'helloFunction');

And then you will have all stub powers!

nrm97
  • 77
  • 2
  • 3
    This isn't really addressing the use case in the question. Westy wants to stub a property, not a function. Plus, it seems the root cause might have been the version he/she was using (as Jonathan indicated). Both the original code in the question and `sinon.stub(myObject, 'hello').value('Sinon')` should work. – carpiediem Jul 30 '19 at 02:18