11

Is there some way I can find all active spies in sinon.js? I'd like to be able to do something like this:

afterEach ->
  sinon.restoreAllSpies()

it "should not create a new MyClass", ->
  spy = sinon.spy(window, 'MyClass')
  expect(spy).not.toHaveBeenCalled()

Currently, I need to laboriously (and error-pronedly!) do this:

it "should not create a new MyClass", ->
  spy = sinon.spy(window, 'MyClass')
  expect(spy).not.toHaveBeenCalled()
  window.MyClass.restore()

Any ideas?

bhuga
  • 1,292
  • 1
  • 14
  • 24

2 Answers2

18

Answer found here: Cleaning up sinon stubs easily

Basically:

sandbox = sinon.sandbox.create()
sandbox.spy(object1, 'methodName')
sandbox.spy(object2, 'methodName')
sandbox.restore()
Community
  • 1
  • 1
Tomas Romero
  • 8,418
  • 11
  • 50
  • 72
6

I dont think so, cause all it does is to replace the function with a spy, it dont save all spies internally. So ether you store all spies in an array and reset them on afterEach, or just create/override new spies on beforeEach.

Andreas Köberle
  • 106,652
  • 57
  • 273
  • 297