很容易清理锡农树桩
Cleaning up sinon stubs easily
有没有一种方法可以轻松重置所有的sinon spys mock和stub,它们将与mocha的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
Sinon通过使用Sandboxes提供此功能,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"
}));
以前的答案建议使用sandboxes
来实现这一点,但根据文档:
自sinon@5.0.0,sinon对象是默认的沙箱。
这意味着清理你的存根/模拟/间谍现在就像一样简单
var sinon = require('sinon');
it('should do my bidding', function() {
sinon.stub(some, 'method');
}
afterEach(function () {
sinon.restore();
});
@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"
}
您可以使用sinon.collection,如sinon库的作者在这篇博客文章(日期为2010年5月)中所示。
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');
}
restore()
只恢复存根功能的行为,但不会重置存根的状态。您必须使用sinon.test
包装测试并使用this.stub
,或者在存根上单独调用reset()
如果你想要一个设置,它会让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)
})
注意,当使用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');
}
);
创建一个沙盒,它将充当所有间谍、存根、mock和fake的黑盒容器。
你所要做的就是在第一个描述块中创建一个沙箱,这样,它就可以在所有测试用例中访问。一旦完成了所有的测试用例,就应该释放原始方法,并使用afterEach钩子中的方法sandbox.restore()
清理存根,以便在运行时释放占用的资源afterEach
测试用例通过或失败。
这里有一个例子:
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();
});
});
将重置所有存根和嵌套的存根。
sinon.reset();
否则你会 NameOfFunctiontionYouWantToReset.resetHistory();
喜欢addingStub.resetHistory();
- 我如何才能让CasperJS和PhantomJS在约塞米蒂玩得很好
- webpack代码拆分了handlerbs文件——结果是文件很大
- 指令加载真的很长,检查加载时间的方法
- 当侧边栏很长时,滚动/固定侧边栏会被切断
- JavaScript数组周围的引号(可能很容易修复)
- 很容易清理锡农树桩
- 尝试将Javascript递归转换为PHP(很容易)
- javascript 冒泡排序问题(可能很容易)
- 如果背景为红色,则jQuery无法识别错误,看起来很容易但很固执
- 不透明度动画应该很容易?(javascript)
- 当使用apply()和call()方法很容易继承时,为什么人们在javascript中使用prototype ?
- 我想删除重复的对象使用javascript/jquery.这可能很容易吗?
- 我怎么能很容易地有一个javascript函数解析出特定的对象从json字符串
- 如何解析多维链接的国家/城市.Json到下拉列表很容易使用javascript或jquery
- 是否有可能很容易地添加一个jquery/javascript鼠标悬停延迟css只下拉菜单
- 在php中url参数名称是否区分大小写?如果不区分大小写,是否可以很容易地将它们规范化(并小写,等等)
- Jquery很容易不收费.js
- Javascript 3D库,可以很容易使用
- Django函数从模板调用?(应该很容易)
- 必须很容易- Div面板文本似乎重新调整中心时,点击以折叠面板