Cleaning up sinon stubs easily

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

    restore() just restores the behavior of the stubbed functionality but it doesn't reset the state of the stubs. You'll have to either wrap your tests with sinon.test and use this.stub or individually call reset() on the stubs

    0 讨论(0)
  • 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"
    } 
    
    0 讨论(0)
  • 2020-12-07 14:11

    Previous answers suggest using sandboxes to accomplish this, but according to the documentation:

    Since sinon@5.0.0, the sinon object is a default sandbox.

    That means that cleaning up your stubs/mocks/spies is now as easy as:

    var sinon = require('sinon');
    
    it('should do my bidding', function() {
        sinon.stub(some, 'method');
    }
    
    afterEach(function () {
        sinon.restore();
    });
    
    0 讨论(0)
  • 2020-12-07 14:12

    Note that when using qunit instead of mocha, you need to wrap these in a module, e.g.

    module("module name"
    {
        //For QUnit2 use
        beforeEach: function() {
        //For QUnit1 use
        setup: function () {
          fakes = sinon.collection;
        },
    
        //For QUnit2 use
        afterEach: function() {
        //For QUnit1 use
        teardown: function () {
          fakes.restore();
        }
    });
    
    test("should restore all mocks stubs and spies between tests", function() {
          stub = fakes.stub(window, 'someFunction');
        }
    );
    
    0 讨论(0)
  • 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"
    }));
    
    0 讨论(0)
  • 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)
    })
    
    0 讨论(0)
提交回复
热议问题