Fluit
Fluit

Reputation: 51

What is the difference between sinon stub.restore and stub.reset

When cleaning up, after creating a stub with sinon, you can restore or reset it. Can someone explain to me what the difference between these two are? Or when to use restore and when to use reset?

Upvotes: 5

Views: 2430

Answers (1)

Delapouite
Delapouite

Reputation: 10187

When you have the following code:

const stub = sinon.stub(object, "foo");

The original object.foo method is gone. If you want to restore it, you can do object.foo.restore() or simply stub.restore() as a shortcut. In other terms, restore has an impact on object.

Whereas stub.reset() has an impact on stub itself, resetting both its behavior and its history. (cache data like how many times it has been called, previous calls…)

Upvotes: 6

Related Questions