title: Using sinon.js spy() to wrap an existing function
body: <p>I'm new to JavaScript testing and have run across a problem when trying to use sinon.js to spy on existing functions.</p>
  
  <p>Let's say I have a function called</p>
  
  <pre><code>nsClientInfectionControlIndex.handleEditButton();
  </code></pre>
  
  <p>which looks like</p>
  
  <pre><code>var nsClientInfectionControlIndex = {
  
  showQtipError : function (message)
  {
      ///&lt;summary&gt;Shows qTip error on edit button&lt;/summary&gt;
      ///&lt;param name="message"&gt;The text message to be displayed&lt;/param&gt;
      $( "#editBtn" ).qtip( nsCBS.ErrorTip( message  ) ).qtip( "show" );
  },
  
  goToEditPage : function (id)
  {
      ///     &lt;summary&gt;Navigates browser window to edit page&lt;/summary&gt;
      ///     &lt;param name="id"&gt;The data-id (Client Infection Control Id) attribute from selected row&lt;/param&gt;
      var url = '/ClientInfectionControl/ClientInfectionControl/'
      window.location.href = url + "?id=" + id;
  },
  
  handleEditButton: function () {
      var id = $( ".selectedRow" ).attr( "data-id" );
      if ( id != null ) {
          nsClientInfectionControlIndex.goToEditPage( id );
      } else {
          nsClientInfectionControlIndex.showQtipError( "Please select a Client Infection Control Note first." );
      }
  },
  
  };
  </code></pre>
  
  <p>Now in my test.js I used the following code</p>
  
  <pre><code>var errorSpy = sinon.spy( nsClientInfectionControlIndex.showQtipError );
  
  //Act
  nsClientInfectionControlIndex.handleEditButton();
  
  //Assert
  equal( errorSpy.called, true, "test" );
  </code></pre>
  
  <p>And the test fails (returns a false) although I would expect a true because the nsClientInfectionControlIndex.showQtipError is called.</p>
  
  <p>Although as far as I understand the Sinon documentation I'm spying on my function correctly by including the function in the constructor. <a href="http://sinonjs.org/docs/#sinonspy" rel="nofollow">http://sinonjs.org/docs/#sinonspy</a></p>
  
  <p>I can get it to work correctly if I approach the spy this way,</p>
  
  <pre><code>var errorSpy = sinon.spy();
  nsClientInfectionControlIndex.showQtipError = errorSpy;
  
  //Act
  nsClientInfectionControlIndex.handleEditButton();
  
  //Assert
  equal( errorSpy.called, true, "test" );
  </code></pre>
  
  <p>However this replaces the original method functionality. Am I approaching this incorrectly? Any help would be appreciated.</p>