title: Faking out FormData append with Jasmine
body: <p>I need to test a unit of code that constructs a FormData object, appends a file and submits an AJAX request to the server. The issue is that I don't actually have a file object to provide to the FormData <code>append</code> method call. So I wanted to spyOn the <code>append</code> method and call a fake function of my own. Is this possible? If not, how can I test my code.</p>
  
  <p>The following is the unit of code I want to test:</p>
  
  <pre><code>var fd = new FormData();
  
  // listOfFiles is an array of files and filename is a string
  var file = _.findWhere(listOfFiles, { name: filename });
  
  fd.append("file", file, filename);
  
  $.ajax(/* options */);
  </code></pre>
  
  <p>The following is what I have for my unit test, which doesn't do the job:</p>
  
  <pre><code>it("does something", function() {
      var mockFiles = [{
          name: "Test File.pdf"
      }];
  
      // the addDocs call adds the files to the listOfFiles array above
      someView.addDocs(mockFiles);
  
      var ajaxSpy = spyOn($, "ajax");
  
      // THIS DOESN'T WORK
      spyOn(FormData.prototype, "append").andReturn(false);
  
      // this event causes the above unit of code to run
      app.trigger('someEvent');
  
      // do more stuff...
  });
  </code></pre>
  
  <p>Right now I get the following error:</p>
  
  <pre><code>Failed to execute 'append' on 'FormData': No function was found that matched the signature provided
  </code></pre>
  
  <p>Which happens because my spy doesn't get called.</p>
  
  <p>Any help is much appreciated how to properly spy on FormData methods.</p>