关于javascript:轻松清理sinon存根

Cleaning up sinon stubs easily

有没有一种方法可以轻松重置所有可与Mocha的beforeEach块完美配合的sinon间谍模拟和存根。

我看到沙盒是一个选项,但是我看不到如何使用沙盒

1
2
3
4
5
6
7
8
9
10
11
12
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

Sinon通过使用沙箱提供了此功能,可以使用两种方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
// 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"
}

要么

1
2
3
4
// 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"
}));


先前的答案建议使用sandboxes完成此操作,但根据文档:

Since [email protected], the sinon object is a default sandbox.

这意味着清理存根/模拟/间谍现在变得很容易:

1
2
3
4
5
6
7
8
9
var sinon = require('sinon');

it('should do my bidding', function() {
    sinon.stub(some, 'method');
}

afterEach(function () {
    sinon.restore();
});


@keithjgrant答案的更新。

从版本v2.0.0起,sinon.test方法已移至单独的sinon-test模块。要使旧的测试通过,您需要在每个测试中配置此额外的依赖项:

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

另外,您可以不使用sinon-test并使用沙箱:

1
2
3
4
5
6
7
8
9
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"
}


您可以使用sinon库的作者在此博客文章(日期为2010年5月)中说明的sinon.collection。

sinon.collection API已更改,并且使用它的方法如下:

1
2
3
4
5
6
7
8
9
10
11
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');
}

如果您想要一个具有sinon的设置,请在所有测试中始终将其重置:

在helper.js中:

1
2
3
4
5
6
7
8
9
10
11
import sinon from 'sinon'

var sandbox;

beforeEach(function() {
    this.sinon = sandbox = sinon.sandbox.create();
});

afterEach(function() {
    sandbox.restore();
});

然后,在您的测试中:

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

restore()只是恢复存根功能的行为,但不重置存根的状态。您必须用sinon.test包装测试并使用this.stub或在存根上单独调用reset()


请注意,当使用qunit而不是mocha时,您需要将它们包装在模块中,例如

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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');
    }
);