Sinon: Spy on and call through a method

Created on 29 Jan 2015  ·  9Comments  ·  Source: sinonjs/sinon

Jasmine has functionality for spying on and calling through to the real version of a method:

spyOn(object, "method").and.callThrough();

This is useful in case of integration tests, where the function should run as usual, but we want to intercept the arguments it is called with.

I cannot find any similar functionality in Sinon -- am I missing something or is this case not supported? (If not, I think it would make a nice addition.)

Thank you in advance.

Most helpful comment

@cjohansen it doesn't state how to get the same result as jasmine's and.callThrough(), which is what I proposed adding an example for :)

All 9 comments

I looked through the API a bit more, and found a solution:

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

Maybe this could be covered in the documentation about stubs as a specific use case?

@cjohansen it doesn't state how to get the same result as jasmine's and.callThrough(), which is what I proposed adding an example for :)

Yes it does. That is what spies are for.

sinon.spy can also spy on existing functions. When doing so, the original function will behave just as normal

sinon.stub(object, "method", object.method) - looks like it returns spy instead of stub, cause it doesn't contain "returns" in chain.
For instance, I want to call through general call, but make a stub for specific
sinon.stub(object, "method", object.method).withArgs().returns() - doesn't work

This is old and so maybe its changed but:

When doing so, the original function will behave just as normal

Isn't what I see.

      it('calls Date with the given birth date if valid date was given', () => {
        let date = sinon.spy(global, 'Date');
        this.formatter('10/10/20');
        expect(date.calledOnce).to.be.true();
        expect(date.getCall(0).args[0]).to.be.equal('10/10/20');
      });

This throws an error that Date.parse is undefined. A method on Date that I use. However, if i simply remove let date = sinon.spy(global, 'Date'); it works (test fails, of course, but no more JS exceptions).

In Jasmine i can do what is referenced above, .callThrough()

@OscarGodson Your issue is only superficially related to the original issue, and deals with the fact that Sinon does not copy properties of the function over to the spy. After playing some with a sandbox, it seems _neither does Jasmine_!

Now I might use Jasmine wrong, but you haven't supplied code that runs standalone, so I'll just have to guess how formatter works (I am assuming it calls Date.parse). No matter if adding callThrough, I still need to add date.parse = orgDate.parse; to not crash on encountering Date.parse later on. In other words, Jasmine and Sinon seem aligned on this issue.

If you think otherwise, please supply some running code, but open a new issue as this has long since been resolved (Sinon even aquired a callThrough of its own in version 2).

Sinon spies perform Jasmine's and.callThrough() by default. I was actually looking for a way to prevent this and found nothing in the documentation. What worked was simply using stub instead of spy.

Yes, that's the difference between spies and stubs in Sinon.

Was this page helpful?
0 / 5 - 0 ratings