Stub API
Creating a stub
sinon.stub()
Creates an anonymous stub function
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()).
Advanced use
sinon.stub(obj)
Stubs all the object's methods.
Note that it's usually better practice to stub individual methods, particularly on objects that you don't understand or control all the methods for (e.g. library dependencies).
Stubbing individual methods shows intent more precisely and is less susceptible to unexpected behavior as the object's code evolves.
sinon.createStubInstance(MyConstructor, overrides)
When you want to create a stub object of MyConstructor, but don't want the constructor to be invoked, use this utility function.
overrides is an optional map overriding created stubs, for example:
is the same as:
If provided value is not a stub, it will be used as the returned value:
is the same as:
Methods
addBehaviorcallArgcallArgWithcallsArgcallsArgAsynccallsArgOncallsArgOnWithcallsArgOnWithAsynccallsArgWithcallsArgWithAsynccallsFakecallsThroughcallThroughWithNewgetonCallonFirstCallonSecondCallonThirdCallrejectsresetresetBehaviorresetHistoryresolvesresolvesArgreturnsreturnsThissetthrowsthrowsArgvaluewithArgsyieldyieldToyieldsyieldsAsyncyieldsOnyieldsOnAsyncyieldsRightyieldsToyieldsToAsyncyieldsToOnyieldsToOnAsync
