Cleaning up sinon stubs easily

我怕爱的太早我们不能终老 提交于 2019-12-17 17:27:16

问题


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 how you can use a sandbox for this

beforeEach ->
  sinon.stub some, 'method'
  sinon.stub some, 'mother'

afterEach ->
  # I want to avoid these lines
  some.method.restore()
  some.other.restore()

it 'should call a some method and not other', ->
  some.method()
  assert.called some.method

回答1:


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"
}));



回答2:


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();
});



回答3:


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"
} 



回答4:


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');
}



回答5:


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)
})



回答6:


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




回答7:


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');
    }
);


来源:https://stackoverflow.com/questions/11552991/cleaning-up-sinon-stubs-easily

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!