Javascript 轻松清理 sinon 存根
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11552991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cleaning up sinon stubs easily
提问by austinbv
Is there a way to easily reset all sinon spys mocks and stubs that will work cleanly with mocha's beforeEach blocks.
有没有办法轻松重置所有 sinon spys 模拟和存根,它们将与 mocha 的 beforeEach 块一起干净地工作。
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
回答by keithjgrant
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"
}
or
或者
// 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"
}));
回答by Myk Willis
Previous answers suggest using sandboxes
to accomplish this, but according to the documentation:
以前的答案建议使用sandboxes
来完成此操作,但根据文档:
Since [email protected], the sinon object is a default sandbox.
从 [email protected] 开始, sinon 对象是默认沙箱。
That means that cleaning up your stubs/mocks/spies is now as easy as:
这意味着清理存根/模拟/间谍现在就像:
var sinon = require('sinon');
it('should do my bidding', function() {
sinon.stub(some, 'method');
}
afterEach(function () {
sinon.restore();
});
回答by anand
An update to @keithjgrant answer.
@keithjgrant 答案的更新。
From version v2.0.0onwards, the sinon.testmethod has been moved to a separate sinon-test
module. To make the old tests pass you need to configure this extra dependency in each test:
从版本V2.0.0起,sinon.test方法已被转移到一个独立的sinon-test
模块。为了让旧的测试通过,你需要在每个测试中配置这个额外的依赖:
var sinonTest = require('sinon-test');
sinon.test = sinonTest.configureTest(sinon);
Alternatively, you do without sinon-test
and use sandboxes:
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"
}
回答by Dimitris Zorbas
You may use sinon.collection as illustrated in thisblog post (dated May 2010) by the author of the sinon library.
您可以使用 sinon.collection,如sinon 库作者的这篇博文(日期为 2010 年 5 月)中所述。
The sinon.collection api has changed and a way to use it is the following:
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');
}
回答by sethcall
If you want a setup that will have sinon always reset itself for all tests:
如果您想要一个让 sinon 始终在所有测试中自行重置的设置:
in helper.js:
在 helper.js 中:
import sinon from 'sinon'
var sandbox;
beforeEach(function() {
this.sinon = sandbox = sinon.sandbox.create();
});
afterEach(function() {
sandbox.restore();
});
Then, in your test:
然后,在您的测试中:
it("some test", function() {
this.sinon.stub(obj, 'hi').returns(null)
})
回答by Sandeep Adi
restore()
just restores the behavior of the stubbed functionality but it doesn't reset the state of the stubs. You'll have to either wrap your tests with sinon.test
and use this.stub
or individually call reset()
on the stubs
restore()
只是恢复存根功能的行为,但不会重置存根的状态。您必须使用sinon.test
并使用this.stub
或单独调用reset()
存根来包装测试
回答by sfuqua
Note that when using qunit instead of mocha, you need to wrap these in a module, e.g.
请注意,当使用 qunit 而不是 mocha 时,您需要将它们包装在一个模块中,例如
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');
}
);
回答by kavigun
Create a sandbox which will act as a black box container for all your spies, stubs, mocks and fakes.
创建一个沙箱,它将充当您所有间谍、存根、模拟和赝品的黑匣子容器。
All you have to do is create a sandbox in the very first describe block so that, it is accessible throughout all the test cases. And once you are done with all the test cases you should release the original methods and clean up the stubs using the method sandbox.restore()
in the afterEach hook so that at runtime it releases held up resources afterEach
test case is passed or failed.
您所要做的就是在第一个 describe 块中创建一个沙箱,以便在所有测试用例中都可以访问它。一旦你完成了所有的测试用例,你应该释放原始方法并使用sandbox.restore()
afterEach 钩子中的方法清理存根 ,以便在运行时它释放保留的资源afterEach
测试用例是通过还是失败。
Here is an example:
下面是一个例子:
describe('MyController', () => {
//Creates a new sandbox object
const sandbox = sinon.createSandbox();
let myControllerInstance: MyController;
let loginStub: sinon.SinonStub;
beforeEach(async () => {
let config = {key: 'value'};
myControllerInstance = new MyController(config);
loginStub = sandbox.stub(ThirdPartyModule, 'login').resolves({success: true});
});
describe('MyControllerMethod1', () => {
it('should run successfully', async () => {
loginStub.withArgs({username: 'Test', password: 'Test'}).resolves();
let ret = await myControllerInstance.run();
expect(ret.status).to.eq('200');
expect(loginStub.called).to.be.true;
});
});
afterEach(async () => {
//clean and release the original methods afterEach test case at runtime
sandbox.restore();
});
});