轻松清理 sinon 存根 [英] Cleaning up sinon stubs easily

查看:33
本文介绍了轻松清理 sinon 存根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以轻松重置所有 sinon spys 模拟和存根,它们将与 mocha 的 beforeEach 块一起干净地工作.

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

推荐答案

Sinon 通过使用 沙箱,可以通过多种方式使用:

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

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

这篇关于轻松清理 sinon 存根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆