2012-03-30 29 views
8

有没有什么方法可以找到sinon.js中所有活跃的间谍?我希望能够做这样的事情:如何在茉莉花每次测试后自动恢复所有sinon.js间谍?

afterEach -> 
    sinon.restoreAllSpies() 

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

目前,我需要辛苦(!和错误pronedly)做到这一点:

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

任何想法?

回答

4

我不这么认为,因为它只是用间谍替换函数,它不会在内部保存所有间谍。因此,您可以将所有间谍存储在数组中,并在afterEach上重置它们,或者在beforeEach上创建/覆盖新的间谍。

+1

何其不幸:( – bhuga 2012-04-18 18:23:38

12

答案在这里找到:Cleaning up sinon stubs easily

基本上是:

sandbox = sinon.sandbox.create() 
sandbox.spy(object1, 'methodName') 
sandbox.spy(object2, 'methodName') 
sandbox.restore()