sinon.spy on function doesn't seem to be working

1,099 views
Skip to first unread message

Hrusikesh Panda

unread,
Oct 29, 2013, 12:53:32 PM10/29/13
to sin...@googlegroups.com
Any idea why the call count is always 0 with the following basic test?

http://jsbin.com/oZUqeTi/1/

var myfunc = function() {
    console.log('hello');
}
  
function myfunc2() {
    console.log('hello2');
}

test("should spy on myfunc", function() {
  
    var mySpy = sinon.spy(myfunc);
    var mySpy2 = sinon.spy(myfunc2);
    myfunc();
    myfunc2();
     
    sinon.assert.calledOnce(mySpy); // false
    sinon.assert.calledOnce(mySpy2);  // false

}); 

Maximilian Antoni

unread,
Oct 29, 2013, 1:15:16 PM10/29/13
to sin...@googlegroups.com
Because you're invoking the original function instead of the spy.

-Max
--
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.

Hrusikesh Panda

unread,
Oct 29, 2013, 3:51:40 PM10/29/13
to sin...@googlegroups.com, maximili...@googlemail.com
Well, in that case, I guess the documentation could use a little help. 

In other words, when you do this: 

sinon.spy(object, "method");

and then invoke object.method, the spy gets invoked transparently. However, in my case, this is not obvious that one needs to invoke the spy rather than the original function. 
Reply all
Reply to author
Forward
0 new messages