Cleaning up sinon stubs easily

前端 未结 8 1012
闹比i
闹比i 2020-12-07 13:38

Is there a way to easily reset all sinon spys mocks and stubs that will work cleanly with mocha\'s beforeEach blocks.

I see sandboxing is an option but I do not see

8条回答
  •  情话喂你
    2020-12-07 14:26

    You may use sinon.collection as illustrated in this blog post (dated May 2010) by the author of the sinon library.

    The sinon.collection api has changed and a way to use it is the following:

    beforeEach(function () {
      fakes = sinon.collection;
    });
    
    afterEach(function () {
      fakes.restore();
    });
    
    it('should restore all mocks stubs and spies between tests', function() {
      stub = fakes.stub(window, 'someFunction');
    }
    

提交回复
热议问题