Skip to content

Fakes API

There are three ways you can create fakes:

  1. Create an empty fake
  2. Wrap an existing function
  3. Use a fake factory

Create an empty fake

A basic fake can be created with no behavior. It saves call information.

Wrap an existing function

You can use sinon.fake to wrap an existing function. This can be used for either observing the system under test, or for creating complex fakes with behaviour. You can pass an arbitrarily complex function to sinon.fake to create custom behavior for a test.

Use a fake factory

The API has a few factories to quickly create fakes with behavior:

Plugging in the fake

Unlike sinon.spy and sinon.stub methods, the sinon.fake API knows only how to create fakes, and doesn't concern itself with plugging them into the system under test.

To plug the fakes into the system under test, you can use the sinon.replace* methods.