我有一套带有摩卡、西农和柴的测试服:
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我不知道自己做错了什么,也不知道使用restore的正确方法是什么。
另外,我也不知道什么时候应该使用重置或恢复。
发布于 2016-08-31 08:40:40
只有在使用以下初始化时,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
});
...
});https://stackoverflow.com/questions/39244242
复制相似问题