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

查看:141
本文介绍了轻松清理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通过使用 Sandboxes 提供此功能,可以使用几种方式:

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天全站免登陆