Sinon: Conflict with doc: sinon.stub(object, "method") doesn't return a object with 'restore' function

Created on 15 Aug 2014  路  7Comments  路  Source: sinonjs/sinon

According to http://sinonjs.org/docs/#stubs

var stub = sinon.stub(object, "method");
Replaces object.method with a stub function. The original function can be restored by calling object.method.restore(); (or stub.restore();). An exception is thrown if the property is not already a function, to help avoid typos when stubbing methods.

IMHO, the stub can be restored by stub.restore(). However, below code fails:

var sinon = require('sinon');

var foo = {
    bar: function() {
    }
}

var mockData = 'hello';

var stub = sinon.stub(foo);
foo.bar.callsArgWith(1, null, mockData);

foo.bar('1.1.1.1', function(err, result) {
    console.log(err);
    console.log(result);

    stub.restore();

    console.log('done');
});

This is the output.

node_modules/sinon/lib/sinon/spy.js:174
                throw exception;
                      ^
TypeError: Object #<Object> has no method 'restore'
    at /Users/chengmo/stash/beeper/test.js:17:7
    at callCallback (/Users/chengmo/stash/beeper/node_modules/sinon/lib/sinon/behavior.js:116:22)
    at Object.proto.invoke (/Users/chengmo/stash/beeper/node_modules/sinon/lib/sinon/behavior.js:139:13)
    at Object.functionStub (/Users/chengmo/stash/beeper/node_modules/sinon/lib/sinon/stub.js:78:61)
    at Function.invoke (/Users/chengmo/stash/beeper/node_modules/sinon/lib/sinon/spy.js:156:55)
    at Object.proxy [as bar] (/Users/chengmo/stash/beeper/node_modules/sinon/lib/sinon/spy.js:82:26)
    at Object.<anonymous> (/Users/chengmo/stash/beeper/test.js:13:5)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)

I suppose this is a bug(version 1.10.3)
If the returned stub can restore all method, it would be great.

All 7 comments

stub.callsArgWith returns a behavior object which has no restore function. It's not a bug, but I see where this in not clear from the documentation. You will have to store the stub in a variable to use restore.

In this case, how to restore stub created by sinon.stub(object)?

Either var s = sinon.stub(obj, 'method'); s.restore(); or obj.method.restore();.

Thanks, but I'm still wondering why sinon.stub(object) doesn't return a object with restore function.

Wouldn't be easier to restore all methods in root object?

It returns the object you stubbed. Changing this is not an option (it break backwards-compatibility). Also, adding methods to the object would be most unexpected. If you need to be able to easily restore lots of stuff, use a sandbox.

@mantoni These doesn't work for me!

@sunfeng90 Please open a new issue describing what exactly does not work for with an example. If you have a usage question, consider posting on the mailing list. Thanks.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sudhirbits picture sudhirbits  路  4Comments

OscarF picture OscarF  路  4Comments

NathanHazout picture NathanHazout  路  3Comments

fearphage picture fearphage  路  3Comments

brettz9 picture brettz9  路  3Comments