Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I automatically restore all sinon.js spies after each test in Jasmine?

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?

like image 555
bhuga Avatar asked Mar 30 '12 21:03

bhuga


1 Answers

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.

like image 62
Andreas Köberle Avatar answered Sep 28 '22 06:09

Andreas Köberle