Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When using Sinon, how to replace stub function in a stub instance?

If I have create a instance by var a = sinon.createStubInstance(MyContructor).

How can I replace one of the stubbed function like var stub = sinon.stub(object, "method", func);.

The main reason I am doing this is want to achieve multiple callback workaround as this mentioned

like image 979
StevenR Avatar asked Oct 13 '15 10:10

StevenR


People also ask

How do I change my Sinon stub?

stub(obj) you can only replace the stub by either assigning a new stub to the property (as described by @g00glen00b) or restoring the stub before re-stubbing. The advantage of this is that you can still call a. method. restore() afterwards with the expected behavior.

How do I use stub function?

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.

How do you mock a function in Sinon?

var mock = sinon. Creates a mock for the provided object. Does not change the object, but returns a mock object to set expectations on the object's methods.

When Spying an original function we need to restore the original function using?

get() method with a spy during setup in the before() function. We restore the function when we teardown the test in the after() function. In the test case, we made the assertion that requestSpy , the object that tracks request. get() 's usage, only records one call.


2 Answers

The method you mentioned (sinon.stub(object, "method", func)) is a method that was available in version 1.x, and did the following according to the documentation:

Replaces object.method with a func, wrapped in a spy. As usual, object.method.restore(); can be used to restore the original method.

However, if you're using sinon.createStubInstance(), all methods are already stubbed. That means you can already do certain things with the stubbed instance. For example:

function Person(firstname, lastname) {
  this.firstname = firstname;
  this.lastname = lastname;
}
Person.prototype.getName = function() {
  return this.firstname + " " + this.lastname;
}

const p = sinon.createStubInstance(Person);
p.getName.returns("Alex Smith");

console.log(p.getName()); // "Alex Smith"

If you really want to replace a stub by another spy or stub, you could assign the property to a new stub or spy:

const p = sinon.createStubInstance(Person);
p.getName = sinon.spy(function() { return "Alex Smith"; }); // Using a spy
p.getName = sinon.stub(); // OR using a stub

With Sinon.js 2.x and higher, it's even easier to replace a stubbed function by using the callsFake() function:

p.getName.callsFake(function() { return "Alex Smith"; });
like image 58
g00glen00b Avatar answered Sep 19 '22 21:09

g00glen00b


After stubbing a whole object using sinon.createStubInstance(MyConstructor) or sinon.stub(obj) you can only replace the stub by either assigning a new stub to the property (as described by @g00glen00b) or restoring the stub before re-stubbing.

var a = sinon.createStubInstance(MyConstructor);
a.method.restore();
sinon.stub(object, "method", func);

The advantage of this is that you can still call a.method.restore() afterwards with the expected behavior.

It would be more convenient if the Stub API had a .call(func) method to override the function being called by the stub after the fact.

like image 35
flungo Avatar answered Sep 20 '22 21:09

flungo