最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Sinon stub callsFake argument - Stack Overflow

programmeradmin4浏览0评论

I had the following stubs running perfectly before

sinon.stub(console, 'log', () => {
    // Check what the arguments holds 
    // And either console.info it or do nothing
});

For example, adding console.info(arguments) inside there, would show me whatever console.log was getting.

With version 2xx I switched to callsFake:

sinon.stub(console, 'log').callsFake(() => {
    // Check what the arguments holds
    // And either console.info it or do nothing
});

This no longer works. console.info(arguments) has bazaar values, and nothing to do with what console.log is passing.

What am I doing wrong?!

I had the following stubs running perfectly before

sinon.stub(console, 'log', () => {
    // Check what the arguments holds 
    // And either console.info it or do nothing
});

For example, adding console.info(arguments) inside there, would show me whatever console.log was getting.

With version 2xx I switched to callsFake:

sinon.stub(console, 'log').callsFake(() => {
    // Check what the arguments holds
    // And either console.info it or do nothing
});

This no longer works. console.info(arguments) has bazaar values, and nothing to do with what console.log is passing.

What am I doing wrong?!

Share Improve this question edited Apr 10, 2023 at 15:52 vsync 130k59 gold badges340 silver badges421 bronze badges asked May 18, 2017 at 21:18 KoushaKousha 36.2k59 gold badges186 silver badges313 bronze badges 1
  • 1 Try replacing callsFake with returns() – zero_cool Commented Oct 3, 2017 at 19:20
Add a comment  | 

1 Answer 1

Reset to default 19

The arrow function you're passing to callsFake doesn't receive the arguments object as you would normally expect in a regular function.

From 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.

Either change your arrow function to a regular anonymous function (function() {...}) or use the spread operator to explicitly unpack the arguments:

sinon.stub(console, 'log')
console.log.callsFake((...args) => {
  console.info(args)
});
发布评论

评论列表(0)

  1. 暂无评论