Cleaning up sinon stubs easily

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

    If you want a setup that will have sinon always reset itself for all tests:

    in helper.js:

    import sinon from 'sinon'
    
    var sandbox;
    
    beforeEach(function() {
        this.sinon = sandbox = sinon.sandbox.create();
    });
    
    afterEach(function() {
        sandbox.restore();
    });
    

    Then, in your test:

    it("some test", function() {
        this.sinon.stub(obj, 'hi').returns(null)
    })
    

提交回复
热议问题