有没有一种方法可以轻松地重置所有 sinon spy 模拟和 stub ,这些模拟和 stub 可以与 mocha 的 beforeEach block 一起干净地工作。
我看到沙盒是一个选项,但我不知道如何为此使用沙盒
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 提供此功能,可以通过几种方式使用:
// 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"
}));
关于javascript - 轻松清理 sinon stub ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11552991/