155

モカのbeforeEachブロックできれいに動作するすべてのシノンスパイモックとスタブを簡単にリセットする方法はありますか?

サンドボックスはオプションだと思いますが、これにサンドボックスを使用する方法がわかりません

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
4

9 に答える 9

318

Sinon はサンドボックスを使用してこの機能を提供します。サンドボックスはいくつかの方法で使用できます。

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

また

// 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"
}));
于 2012-12-26T13:55:40.393 に答える
100

以前の回答はsandboxes、これを達成するために使用することを提案していますが、ドキュメントによると:

sinon@5.0.0 以降、sinon オブジェクトはデフォルトのサンドボックスです。

つまり、スタブ/モック/スパイのクリーンアップが次のように簡単になりました。

var sinon = require('sinon');

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

afterEach(function () {
    sinon.restore();
});
于 2019-03-19T23:45:47.753 に答える
14

@keithjgrant の回答の更新。

バージョンv2.0.0以降、sinon.testメソッドは別のsinon-testモジュールに移動されました。古いテストに合格するには、各テストでこの追加の依存関係を構成する必要があります。

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

または、サンドボックスsinon-testを使用せずに次のようにします。

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"
} 
于 2017-04-19T03:30:13.833 に答える
9

sinon ライブラリの作成者によるこのブログ投稿 (2010 年 5 月付け) に示されているように、sinon.collection を使用できます。

sinon.collection api が変更されました。使用方法は次のとおりです。

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');
}
于 2013-07-11T21:36:46.323 に答える
6

sinon が常にすべてのテストで自分自身をリセットする設定が必要な場合:

helper.js で:

import sinon from 'sinon'

var sandbox;

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

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

次に、テストで:

it("some test", function() {
    this.sinon.stub(obj, 'hi').returns(null)
})
于 2016-07-18T12:41:47.857 に答える
3

mocha の代わりに qunit を使用する場合は、これらをモジュールでラップする必要があることに注意してください。

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');
    }
);
于 2014-04-18T14:54:07.847 に答える