Cleaning up sinon stubs easily

javascript, mocha.js, sinon, stubbing, testing

Solution

Sinon provides this functionality through the use of Sandboxes, which can be used a couple ways:

// manually create and restore the sandbox
var sandbox;
beforeEach(function () {
    sandbox = sinon.sandbox.create();
});

afterEach(function () {
    sandbox.restore();
});

it('should restore all mocks stubs and spies between tests', function() {
    sandbox.stub(some, 'method'); // note the use of "sandbox"
}

or

// wrap your test function in sinon.test()
it("should automatically restore all mocks stubs and spies", sinon.test(function() {
    this.stub(some, 'method'); // note the use of "this"
}));

Problem

Is there a way to easily reset all sinon spys mocks and stubs that will work cleanly with mocha's beforeEach blocks. I see sandboxing is an option but I do not see how you can use a sandbox for this ``` beforeEach -> sinon.stub some, 'method' sinon.stub some, 'mother' afterEach -> # I want to avoid these lines some.method.restore() some.other.restore() it 'should call a some method and not other', -> some.method() assert.called some.method ```

Original source