title: Test the context of a method call using sinonjs
body: <p>I have a class where I bind a method on initialization  as follows - </p>
  
  <pre><code>function MyClass() {
      this.onHttpCallback = _.bind(onHttpCallback, this);
  }
  
  function onHttpCallback(){
     //... 
  }
  </code></pre>
  
  <p>How do I test if the <code>onHttpCallback</code> when called, is always called with an object of MyClass as context?</p>
  
  <p>I am using sinon.js to mock and the following code doesn't work - </p>
  
  <pre><code>it('should be binded', function () {
      //ctrl is an object of MyClass
      var dummy_obj = {};
      var spy = sinon.spy(ctrl.onHttpCallback);
      spy.call(dummy_obj);
      spy.alwaysCalledOn(ctrl).should.be.ok;
  });
  </code></pre>
  
  <p><strong>Update</strong>
  As per the comments in the following answer, it seems like it is impossible to test the binding for a method.</p>
  
  <p><strong>My Take on the problem</strong></p>
  
  <pre><code>//Source.js
  
  function MyClass() {
  
  }
  
  MyClass.prototype.init = function(){
     this.onHttpCallback = _.bind(MyClass.onHttpCallback, this);
  }
  
  
  MyClass.onHttpCallback(){
     //... 
  }
  
  //Test.js
  
  it('should bind onHttpCallback', function () {
      sinon.spy(_, 'bind');
      ctrl.init();
      _.bind.calledWith(ctrl.constructor.onHttpCallback, ctrl).should.be.ok;
      _.bind.restore();
  });
  </code></pre>
  
  <p>Works like a charm!</p>