Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 836
user2503038 Avatar asked May 27 '14 14:05

user2503038


1 Answers

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

like image 172
Nilesh Khisadiya Avatar answered Sep 28 '22 05:09

Nilesh Khisadiya