title: Backbone Models not updating after Sinon.fakeServer responds to model.fetch
body: <p>I am attempting to write a test suite for our Backbone app. We are using Jest as the testing framework, and Sinon for server mocking. </p>
  
  <p>An example test looks like</p>
  
  <p><strong>device.test.js</strong></p>
  
  <pre><code>describe('API integration', ()=&gt;{
          let response = {"data": {
                  "attributes": {
                      "created_at": "2018-06-14T07:05:14.919Z",
                  },
                  "id": "234",
                  "type": "device"
           }};
  
          let server = sinon.fakeServer.create();
          afterAll(() =&gt; {
              server.restore();
          });
  
          let device = new Device({
              id: '234'
              created_at: null,
              agent_code: null,
              device_code: null,
          });
  
          server.respondWith('GET',
              'api/device/234',
              [200, {"Content-Type": "application/json"},
              JSON.stringify(response)]);
  
          device.fetch();
          server.respond();
          it('fetches to the right URL', ()=&gt;{
              // these both pass
              expect(server.requests[0].method === 'GET');
              expect(server.requests[0].url === 'api/device/234');
          });
          it('updates its fields properly after a fetch', ()=&gt;{
             // this test fails
             expect(device.get('created_at')).toBe(response.attributes.created_at);
  
          });           
      });
  </code></pre>
  
  <p>I was using <a href="https://tinnedfruit.com/articles/testing-backbone-apps-with-jasmine-sinon-2.html" rel="nofollow noreferrer">this article</a> as an example of how to test backbone models with sinon (scroll towards nearly the bottom for fakeserver usage) as well as using the <a href="http://sinonjs.org/releases/v6.0.0/fake-xhr-and-server/" rel="nofollow noreferrer">sinon fakeserver docs</a>. </p>
  
  <p>According to what I've read, <code>server.respond()</code> should evaluate all async requests, and thus the models should be updated synchronously. That is to say, my expectation is that any references to <code>device</code> after <code>server.respond</code> should be referring to a post-fetch <code>device</code> model. My expectation is not met - after a call to <code>server.respond()</code>, <code>device.get('created_at')</code> remains <code>undefined</code>. This is not the case in production, when our app is running. </p>
  
  <p>Just in case, I tried to throw tests into a <code>{success: function(){}}</code> callback, as well as in <code>.then</code> and <code>.done</code>, but then the tests wouldn't run, nor would <code>console.log()</code> or any other debugging efforts. </p>
  
  <p><strong>How can I test asynchronous methods such as <code>Backbone.model.fetch()</code> in Jest using Sinon.fakeServer?</strong></p>
  
  <p>EDIT: <a href="https://stackoverflow.com/questions/26368140/backbone-model-not-updating-with-server-data">This answer</a> made me wonder if maybe <code>parse</code> was even being called - perhaps I am structuring my return data abnormally. Parse is indeed being called, so now I suspect this is not an issue with asynchronousness, but instead with how I am structuring my data in the <code>response</code> object. </p>