admin管理员组文章数量:1208155
I have a test suit with mocha, sinon and chai:
describe('general visor methods tests', () => {
let res, req, next, resSpy, resNext;
beforeEach(() => {
res = {};
next = () => {};
resSpy = res.json = sinon.spy();
resNext = next = sinon.spy();
});
afterEach(() => {
resSpy.restore();
resNext.reset();
});
describe('get basemap layers from owner model', () => {
it('should send the basemap provided by the owner model', () => {
owner.basemap = ['basemap1', 'basemap2'];
getBaseMapLayersFromConfig(req, res, next);
// console.log(resSpy.args[0][0].data);
expect(resSpy.calledOnce).to.eql(true);
expect(resSpy.args[0][0].message).to.eql('basemaps correctly found');
expect(resSpy.args[0][0].data).to.eql(['basemap1', 'basemap2']);
});
...
if I put resSpy.reset()
it works fine. I've read that the reset() function is to reset the state of the spy.
But what i don't understand is that if i put resSpy.restore() then it thows the next error:
TypeError: resSpy.restore is not a function
I don't know what I'm doing wrong or what should be the correct way of using restore.
Also I don't quite know when should i use reset or restore.
I have a test suit with mocha, sinon and chai:
describe('general visor methods tests', () => {
let res, req, next, resSpy, resNext;
beforeEach(() => {
res = {};
next = () => {};
resSpy = res.json = sinon.spy();
resNext = next = sinon.spy();
});
afterEach(() => {
resSpy.restore();
resNext.reset();
});
describe('get basemap layers from owner model', () => {
it('should send the basemap provided by the owner model', () => {
owner.basemap = ['basemap1', 'basemap2'];
getBaseMapLayersFromConfig(req, res, next);
// console.log(resSpy.args[0][0].data);
expect(resSpy.calledOnce).to.eql(true);
expect(resSpy.args[0][0].message).to.eql('basemaps correctly found');
expect(resSpy.args[0][0].data).to.eql(['basemap1', 'basemap2']);
});
...
if I put resSpy.reset()
it works fine. I've read that the reset() function is to reset the state of the spy.
But what i don't understand is that if i put resSpy.restore() then it thows the next error:
TypeError: resSpy.restore is not a function
I don't know what I'm doing wrong or what should be the correct way of using restore.
Also I don't quite know when should i use reset or restore.
Share Improve this question asked Aug 31, 2016 at 8:17 sendrasendra 6882 gold badges9 silver badges21 bronze badges1 Answer
Reset to default 21spy.restore()
is only useful if you're using the following initialization:
let someSpy = sinon.spy(obj, 'someFunction');
This will replace obj.someFunction
with the spy. If you ever want to go back to the original, you use someSpy.restore()
.
You're using a standalone spy, so there's nothing to restore.
Also, because you're creating new spies for each test, in beforeEach
, you don't have to reset anything in afterEach
. That's only useful if you want to reuse the spies:
describe('general visor methods tests', () => {
let someSpy = sinon.spy(); // create the spy once
afterEach(() => {
someSpy.reset(); // reset after each test
});
...
});
本文标签: javascriptWhat is the correct way of using sinon spy restore or resetStack Overflow
版权声明:本文标题:javascript - What is the correct way of using sinon spy restore or reset? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738750027a2110344.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论