Sinon.js spy does not work correctly in mocha

I have a mocha test case, where I want to spy on an existing functions. Unfortunately sinon.js does not count the calls:

    it("should work with spy", function() {
    var func = function() {
    };

    var spy = sinon.spy(func);

    func();
    func();

    console.log(spy.callCount);

    // fails
    assert(spy.CallCount == 2);
    /* works:
    var secondSpy = sinon.spy();

    secondSpy();

    console.log(secondSpy.callCount);
    */
});

What could be probably be wrong?

The problem is you need to call the spy like you do in the working example. The only instance (see line 21) where the function can be called is when it's inside of an object (e.g. sinon.spy(jQuery, "ajax")) as it will overwrite it with the spy properties. In the other two cases where you can create a spy it will only return a spy that mimics the original function.

But this should be quite simple for testing modules as you can use the method I previously mentioned - sinon.spy(moduleName, "methodToSpy");. Then when you call that method directly, you will also be able to get the spy properties. To then remove the spy properties you would need to call moduleName.methodToSpy.restore().