使用 sinon spy restore 或 reset 的正确方法是什么?
What is the correct way of using sinon spy restore or reset?
我有一套mocha、sinon和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']);
});
...
如果我输入 resSpy.reset()
它工作正常。我看过 reset() 函数是重置间谍的状态。
但我不明白的是,如果我输入 resSpy.restore() 那么它会抛出下一个错误:
TypeError: resSpy.restore is not a function
我不知道自己哪里做错了,也不知道正确的恢复方法是什么。
我也不太清楚什么时候应该使用重置或恢复。
spy.restore()
仅在您使用以下初始化时才有用:
let someSpy = sinon.spy(obj, 'someFunction');
这将用间谍替换 obj.someFunction
。如果您想回到原来的状态,请使用 someSpy.restore()
。
您使用的是独立间谍,因此没有可恢复的内容。
此外,因为您在 beforeEach
中为每个测试创建新间谍,所以您不必在 afterEach
中重置任何内容。这只有在你想重用间谍时才有用:
describe('general visor methods tests', () => {
let someSpy = sinon.spy(); // create the spy once
afterEach(() => {
someSpy.reset(); // reset after each test
});
...
});
我有一套mocha、sinon和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']);
});
...
如果我输入 resSpy.reset()
它工作正常。我看过 reset() 函数是重置间谍的状态。
但我不明白的是,如果我输入 resSpy.restore() 那么它会抛出下一个错误:
TypeError: resSpy.restore is not a function
我不知道自己哪里做错了,也不知道正确的恢复方法是什么。
我也不太清楚什么时候应该使用重置或恢复。
spy.restore()
仅在您使用以下初始化时才有用:
let someSpy = sinon.spy(obj, 'someFunction');
这将用间谍替换 obj.someFunction
。如果您想回到原来的状态,请使用 someSpy.restore()
。
您使用的是独立间谍,因此没有可恢复的内容。
此外,因为您在 beforeEach
中为每个测试创建新间谍,所以您不必在 afterEach
中重置任何内容。这只有在你想重用间谍时才有用:
describe('general visor methods tests', () => {
let someSpy = sinon.spy(); // create the spy once
afterEach(() => {
someSpy.reset(); // reset after each test
});
...
});