Articles

How do I stub with Sinon?

How do I stub with Sinon?

If you need to check that a certain value is set before a function is called, you can use the third parameter of stub to insert an assertion into the stub: var object = { }; var expectedValue = ‘something’; var func = sinon. stub(example, ‘func’, function() { assert. equal(object.

What is Sinon fake?

In Sinon, a fake is a Function that records arguments, return value, the value of this and exception thrown (if any) for all of its calls. A fake is immutable: once created, the behavior will not change. Unlike sinon.

What is stub in Sinon?

Test stubs are functions (spies) with pre-programmed behavior. They support the full test spy API in addition to methods which can be used to alter the stub’s behavior. As spies, stubs can be either anonymous, or wrap existing functions.

What is Sinon Spy ()?

sinon.spy(object, “method”) creates a spy that wraps the existing function object.method . The spy will behave exactly like the original method (including when used as a constructor), but you will have access to data about all calls.

When do you use a stub in Sinon?

Use stubs when you want to control the method behaviour, for instance force a method to throwing an exception to check the error handling functionalities. prevent a method to get call directly to stop triggering undesired behaviour (sinon stub documentaion).

What’s the difference between yields and callsarg in Sinon?

Note that in Sinon version 1.5 to version 1.7, multiple calls to the yields* and callsArg* family of methods define a sequence of behaviors for consecutive calls. As of 1.8, this functionality has been removed in favor of the onCall API. Creates an anonymous stub function Replaces object.method with a stub function.

How to make a stub respond differently on consecutive calls?

As of Sinon version 1.8, you can use the onCall method to make a stub respond differently on consecutive calls. Note that in Sinon version 1.5 to version 1.7, multiple calls to the yields* and callsArg* family of methods define a sequence of behaviors for consecutive calls. As of 1.8, this functionality has been removed in favor of the onCall API.

What does a fake do in Sinon V5?

fake was introduced with Sinon with v5. It simplifies and merges concepts from spies and stubs. In Sinon, a fake is a Function that records arguments, return value, the value of this and exception thrown (if any) for all of its calls. It can be created with or without behavior; it can wrap an existing function.