Skip to content

Instantly share code, notes, and snippets.

@jfirebaugh
Created April 25, 2012 18:19
Show Gist options
  • Save jfirebaugh/2491871 to your computer and use it in GitHub Desktop.
Save jfirebaugh/2491871 to your computer and use it in GitHub Desktop.
Auto-restoring sinon fakes with Konacha
# require spec_helper
Test =
fn: -> 'result'
it 'auto-resets sinon fakes', ->
@stub(Test, 'fn')
Test.fn()
Test.fn.should.have.been.called
it 'has been reset', ->
Test.fn().should.equal('result')
# Injected by sinon.js
Konacha.mochaOptions.globals = [
'XMLHttpRequest'
'setTimeout'
'setInterval'
'clearTimeout'
'clearInterval'
]
#= require support/sinon
#= require support/sinon-chai
window.chai.use(window.sinonChai)
beforeEach ->
@sandbox = sinon.sandbox.create
injectInto: this
properties: ["spy", "stub", "mock", "clock", "server", "requests"],
useFakeTimers: true,
useFakeServer: true
afterEach ->
@sandbox.restore()
@joliss
Copy link

joliss commented Apr 25, 2012

Interesting, thanks for posting your solution.

Btw, since you are using CoffeeScript, which auto-adds var, I assume it's safe to just enable ignoreLeaks instead of maintaining a globals list.

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