sinon.stub() vs sinon.sandbox.stub()?

user2503038 picture user2503038 · May 27, 2014 · Viewed 8.8k times · Source

Using sinon and sinon-qunit in our front end unit tests, and I'm struggling to understand the difference in these methods. We are using sinon.sandbox.stub() (literally that is the function, we do not create a sandbox) and these stubs are apparently restored after each test automatically. I just don't see this anywhere in the documentation.

I wouldn't think that this method exists, I would think you would need to explicitly create a sandbox using sinon.sandbox.create(). On that sandbox object you would call the stub function, i.e. mySandbox.stub(), not "sinon.sandbox.stub()".

Could anyone help me understand?

Answer

Nilesh Khisadiya picture Nilesh Khisadiya · Aug 7, 2017

Stubs - Sinon.JS

sinon.stub(); read about from here


Sandboxes - Sinon.JS

sandbox.stub(); read detail from here

Works almost exactly like sinon.stub, only also adds the returned stub to the internal collection of fakes for easy restoring through sandbox.restore().

The sandbox stub method can also be used to stub any kind of property. This is useful if you need to override an object’s property for the duration of a test, and have it restored when the test completes