10
votes

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?

1
Are you sure you don't call .restore() in an after() block? The only thing it does extra is that it adds the stub to an internal list which, when call .restore() it restores all of the stubs inside. - Henrik Andersson
We definitely don't call restore anywhere. My guess is the sinon-qunit framework is doing that behind the scenes. - user2503038
According to the documentation it automatically restore all stubbed/spied jQuery.ajax fakes. - Henrik Andersson

1 Answers

6
votes

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