20

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');
2
  • What is the value of myObject? { hello: 'world'}? Nov 21, 2017 at 23:12
  • It's failing in the compile step, it's not running.
    – Westy
    Nov 27, 2017 at 15:49

2 Answers 2

35

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"
2
  • 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, 2017 at 23:14
  • stub.value was added in Sinon v2, FWIW.
    – Tgr
    Sep 4, 2022 at 5: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!

1
  • 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, 2019 at 2:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.