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

Is there a way to find all active spies in sinon.js? I would like 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 do this with difficulty (and with an error!):

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

Any ideas?

+8
javascript jasmine sinon
source share
2 answers

I don’t think so, because all he does is replace the function with a spy, it does not save all the spies inside. So, you store all the spies in the array and reset them to afterEach, or just create / redefine new spies on beforeEach.

+4
source share

The answer can be found here: Easy to clean sinon stubs

Basically:

 sandbox = sinon.sandbox.create() sandbox.spy(object1, 'methodName') sandbox.spy(object2, 'methodName') sandbox.restore() 
+12
source share

All Articles