title: Sinon.js stub and test external function call with object as parameter later modified by ref
body: <p>I'm looking to test the call my function makes to another function, particularly an argument that is an object.  </p>
  
  <p>The issue is that sinon.js seems to store a reference to the object parameter given in its arguments array, which is naturally expected.  However, this creates a problem when the parameter is later modified by ref, which changes the <em>apparent</em> value of those parameters at the time the function was called.</p>
  
  <p>What's the best way to unit test a situation like this?</p>
  
  <p>Here is a contrived example:</p>
  
  <h2><a href="http://jsfiddle.net/xtfQu/" rel="nofollow">http://jsfiddle.net/xtfQu/</a></h2>
  
  <pre><code>var view = Backbone.View.extend({
      initialize: function () {
          _.bindAll(this);    
      },
  
      dostuff: function () {
          var coords = { x: 0, y: 0 };
          for (var i = 0; i &lt; 10; i++) {
              this.otherstuff(coords);
  
              coords.x += 10;
          }
      },
  
      otherstuff: function (coord) {
          // Stubbed
      }
  });
  
  test("a test", function() {
      // Arrange
      var blah = new view();
  
      sinon.stub(blah, 'otherstuff');
  
      // Act
      blah.dostuff();
  
      // Assert
      var expectedFirstCallCoords = { x: 0, y: 0 };
  
      // All assertions on the value of x for a particular call would seem to be impossible.
      // blah.otherstuff.firstCall.args[0].x --&gt; 100
      deepEqual(blah.otherstuff.firstCall.args[0], expectedFirstCallCoords, 'parameter changed by ref, untestable?');
  });​
  </code></pre>
  
  <p>I can think of various hacks around this 'issue'.  Is there a cleaner approach that doesn't involve cloning the object or modifying my production code simply for the sake of getting sinon to work?</p>