Using JavaScript and sinon How do you spy on methods called in the constructor?

I really need help with the following code - This is not pasted from my program its off the top of my head but I think it clearly demonstrates the problem (and I believe it to be fully accurate). When I request the value of "spy.called" it ignores the call made in the constructor function. How do I code this so that the call inside the constructor is registered by the spy?

OR if not possible what approach should I take? Example code greatly appreciated - Many thanks - Been banging my head all day with this!

function MyClass() {
  var self = this;
  this.myFunc = function() {
    console.log("hi");
  }
  function init() {
    self.myFunc();
  }
  init();
}


var spy = sinon.spy(new MyClass(), "myFunc");
console.log(spy.called);  // true if the spy was called at least once
// ABOVE OUTPUTS FALSE - IT FAILS TO REGISTER THE CALL IN THE CONSTRUCTOR!
spy.myFunc();
console.log(spy.called);
// ABOVE OUTPUTS TRUE AS EXPECTED

The problem here is that when the method myFunc is called the spy doesn't exist yet. Your code is equivalent to :

var c = new MyClass()
var spy = sinon.spy(c, "myFunc");

Clearly the spy is not in place when the constructor is called.

To solve this problem you can move the method myFunc in the prototype of the MyClass object and then spy the methods in the prototype.

For example:

function MyClass() {
  this.init();
}

MyClass.prototype.myFunc = function() {
    console.log("hi");
}

MyClass.prototype.init = function() {
   this.myFunc();
}

var myFuncSpy = sinon.spy(MyClass.prototype, "myFunc");
var initSpy = sinon.spy(MyClass.prototype, "init");

var c = new MyClass();
console.log(myFuncSpy.called); // TRUE
console.log(initSpy.called);  // TRUE

JSFIDDLE: http://jsfiddle.net/och191so/1/ Open the console to see the result.

I think you should redesign your class a bit. You can either accept myFunc in constructor argument (only if it makes sense from point of usage) or you can set it on MyClass' prototype:

function MyClass() {
    function init() {
        this.myFunc();
    }
    init();
}

MyClass.prototype.myFunc = function() {
    console.log("hi");
}

var spy = sinon.spy(MyClass.prototype, "myFunc");
new MyClass();
console.log(spy.called);