Sinon 存根调用假参数
Sinon stub callsFake argument
我之前 运行 完美地拥有以下存根
sinon.stub(console, 'log', () => {
// Check what the arguments holds
// And either console.info it or do nothing
});
例如,在其中添加 console.info(arguments)
,会显示 console.log
得到的结果。
使用版本 2xx
我切换到 callsFake
:
sinon.stub(console, 'log').callsFake(() => {
// Check what the arguments holds
// And either console.info it or do nothing
});
这现在不再有效了。 console.info(arguments)
具有市场价值,与 console.log
传递的内容无关。
我做错了什么?!
您传递给 callsFake
的箭头函数没有像您通常在常规函数中期望的那样接收 arguments
对象。
来自MDN
An arrow function expression has a shorter syntax than a function expression and does not have its own this, arguments, super, or new.target.
要么将箭头函数更改为常规匿名函数 (function() {...}
),要么使用扩展运算符显式解压缩参数:
sinon.stub(console, 'log')
console.log.callsFake((...args) => {
console.info(args)
});
我之前 运行 完美地拥有以下存根
sinon.stub(console, 'log', () => {
// Check what the arguments holds
// And either console.info it or do nothing
});
例如,在其中添加 console.info(arguments)
,会显示 console.log
得到的结果。
使用版本 2xx
我切换到 callsFake
:
sinon.stub(console, 'log').callsFake(() => {
// Check what the arguments holds
// And either console.info it or do nothing
});
这现在不再有效了。 console.info(arguments)
具有市场价值,与 console.log
传递的内容无关。
我做错了什么?!
您传递给 callsFake
的箭头函数没有像您通常在常规函数中期望的那样接收 arguments
对象。
来自MDN
An arrow function expression has a shorter syntax than a function expression and does not have its own this, arguments, super, or new.target.
要么将箭头函数更改为常规匿名函数 (function() {...}
),要么使用扩展运算符显式解压缩参数:
sinon.stub(console, 'log')
console.log.callsFake((...args) => {
console.info(args)
});