Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restore single stub in sandbox

sandbox = sinon.sandbox.create();

sandbox.stub(db, 'query', () => {
    return Promise.resolve();
});

sandbox.stub(process, 'exit', () => { });

sandbox.restore(); removes all the stubs.

I want to remove ONE stub so I can restub it. For example the query stub.

Is this possible? I can't find any information on this.

like image 959
basickarl Avatar asked Nov 23 '16 10:11

basickarl


People also ask

How do I restore my Sinon stub?

var stub = sinon. The original function can be restored by calling object. method. restore(); (or stub. restore(); ).

What is Sandbox restore?

restore(); Restores all fakes created through sandbox.

How do you stub a function in Sinon?

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. For example, the below code stubs out axios.


1 Answers

You can restore single method like this:

db.query.restore();

for your particular case.

Per sinon documentation:

var stub = sinon.stub(object, "method");

Replaces object.method with a stub function. An exception is thrown if the property is not already a function.

The original function can be restored by calling object.method.restore(); (or stub.restore();).

See http://sinonjs.org/releases/v2.3.6/stubs/

like image 100
punov Avatar answered Sep 21 '22 16:09

punov