Back to Sinon

Fakes

docs/concepts/fakes/index.md

22.1.01.3 KB
Original Source

Fakes

Introduction

sinon.fake allows creation of a fake Function with the ability to set a default behavior.

In Sinon, a fake is a Function that records arguments, return value, the value of this and Error thrown (if any) for all of its calls.

A fake is an immutable object: once created, its behavior will not change.

Basic usage

<<< ../../.vitepress/tests/docs/fakes/basic-usage.test.js

Prefer fakes over spies and stubs

Fakes are alternatives to the older spies and stubs, and can replace them in all use cases.

They are designed to be simpler and easier to use, while avoiding confusion by being immutable.

All fakes have the same API as spies. This includes access to call information through the spy call API, such as firstArg, lastArg, and callback properties.

Using fakes instead of spies

<<< ../../.vitepress/tests/docs/fakes/_index-1.test.js

Using fakes instead of stubs

<<< ../../.vitepress/tests/docs/fakes/_index-2.test.js