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
Sinon provides this functionality through the use of Sandboxes, which can be used a couple ways:
or
Note that when using qunit instead of mocha, you need to wrap these in a module, e.g.
You may use sinon.collection as illustrated in this blog post (dated May 2010) by the author of the sinon library.
The sinon.collection api has changed and a way to use it is the following:
An update to @keithjgrant answer.
From version v2.0.0 onwards, the sinon.test method has been moved to a separate
sinon-test
module. To make the old tests pass you need to configure this extra dependency in each test:Alternatively, you do without
sinon-test
and use sandboxes:restore()
just restores the behavior of the stubbed functionality but it doesn't reset the state of the stubs. You'll have to either wrap your tests withsinon.test
and usethis.stub
or individually callreset()
on the stubsIf you want a setup that will have sinon always reset itself for all tests:
in helper.js:
Then, in your test: