Cleaning up sinon stubs easily

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

    Sinon provides this functionality through the use of Sandboxes, which can be used a couple ways:

    // manually create and restore the sandbox
    var sandbox;
    beforeEach(function () {
        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"
    }
    

    or

    // wrap your test function in sinon.test()
    it("should automatically restore all mocks stubs and spies", sinon.test(function() {
        this.stub(some, 'method'); // note the use of "this"
    }));
    

提交回复
热议问题