Is it possible to spy a function called through Function.prototype.bind()?

1,006 views
Skip to first unread message

Olivier Audard

unread,
Aug 15, 2013, 5:36:04 PM8/15/13
to sin...@googlegroups.com
Hi,

I was giving a try to Sinon.JS on a small personal project and I faced the following problem:

A spy doesn't seem to be updated when the spied function is called through a bound function.

Here is a reduced test case : http://jsfiddle.net/dhar/ZgQja/

I can't figure out what's wrong with this implementation.
Any help on how to test this particular case will be appreciated.

Thanks.



Uzi Kilon

unread,
Aug 15, 2013, 5:57:09 PM8/15/13
to sin...@googlegroups.com
bind returns a new function that will run the original function in a given context.

if you look at it as a wrapper, it would be easier to figure out what is going on.
so if you spy on the original function and invoke the wrapper one, the spy will be called.
however if you spy on the wrapper and call the original function, it should not work.


--
You received this message because you are subscribed to the Google Groups "Sinon.JS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sinonjs+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Olivier Audard

unread,
Aug 15, 2013, 6:37:55 PM8/15/13
to sin...@googlegroups.com
Thanks for the (really) quick reply. ^^

I figured this out right after posting my question. Defining the spy before the bound function (testFunc) make the test pass. It makes sense.

That said, I'm trying to find a way to test if a bound function actually calls the correct original function.
I guess that won't be as easy as I though ;)
Reply all
Reply to author
Forward
0 new messages