Cleaning up sinon stubs easily

前端 未结 8 1019
闹比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:07

    An update to @keithjgrant answer.

    From version v2.0.0 onwards, the sinon.test method has been moved to a separate sinon-test module. To make the old tests pass you need to configure this extra dependency in each test:

    var sinonTest = require('sinon-test');
    sinon.test = sinonTest.configureTest(sinon);
    

    Alternatively, you do without sinon-test and use sandboxes:

    var sandbox = sinon.sandbox.create();
    
    afterEach(function () {
        sandbox.restore();
    });
    
    it('should restore all mocks stubs and spies between tests', function() {
        sandbox.stub(some, 'method'); // note the use of "sandbox"
    } 
    

提交回复
热议问题