Skip to content

Instantly share code, notes, and snippets.

@jgable
Created May 8, 2014 15:29
Show Gist options
  • Save jgable/fd7fbd0516c849731404 to your computer and use it in GitHub Desktop.
Save jgable/fd7fbd0516c849731404 to your computer and use it in GitHub Desktop.
Sinon Sandbox Example
var sinon = require('sinon'),
Widget = require('../../widget');
describe('My widget', function () {
var sandbox;
beforeEach(function () {
// Create a sandbox for the test
sandbox = sinon.sandbox.create();
});
afterEach(function () {
// Restore all the things made through the sandbox
sandbox.restore();
});
it('is awesome', function () {
var widget = new Widget();
// Make sure to only create stubs/spies through the sandbox
widget.fetch = sandbox.stub().returns({ one: 1, two: 2 });
widget.loadData();
expect(widget.fetch.called).to.be.true;
});
})
@SCdF
Copy link

SCdF commented Apr 20, 2017

I realise this is some real old code… but FWIW you don't need to create the sandbox each time, restore is putting it back to how it should be.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment