Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sinon stub callsFake argument

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 now longer works. console.info(arguments) has bazaar values, and nothing to do with what console.log is passing.

What am I doing wrong?!

like image 664
Kousha Avatar asked May 18 '17 21:05

Kousha


People also ask

What does Sinon stub return?

The sinon. stub() substitutes the real function and returns a stub object that you can configure using methods like callsFake() . Stubs also have a callCount property that tells you how many times the stub was called.

Which of the following Sinon test function that records arguments return value the value of this and exception thrown if any for all its calls?

A test spy is a function that records arguments, return value, the value of this and exception thrown (if any) for all its calls.

How does Sinon stub work?

Sinon replaces the whole request module (or part of it) during the test execution, making the stub available via require('request') and then restore it after the tests are finished? require('request') will return the same (object) reference, that was created inside the "request" module, every time it's called.


1 Answers

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)
});
like image 99
philipisapain Avatar answered Oct 09 '22 10:10

philipisapain