title: Testing Promises/Async flow with Sinon.js
body: <p>I am having trouble testing the returned value of a function that waits for a promise to be resolved before executing.</p>
  
  <p><strong>Javascript Method</strong> (serviceClient._getProduct returns a jQuery ajax promise object)</p>
  
  <pre><code>serviceClient.getProductName = function(id, storeId) {
    $.when(self._getProduct(id)).done(function(data) {
      return data.name;
    });
  };
  </code></pre>
  
  <p><strong>Test Code</strong></p>
  
  <pre><code>before(function() {
    serviceClient.sampleResponse = fixture.load('product_response.json')[0];
    $.ajaxStub = sinon.stub($, 'ajax').returns(new $.Deferred().resolve(serviceClient.sampleResponse));
  });
  
  describe('.getProductName', function() {
    it('should return the product name', function() {
      var name = serviceClient.getProductName(serviceClient.sampleResponse.id);
  
      $.when($.ajaxStub.returnValue).done(function() {
        expect(name).to.equal(serviceClient.sampleResponse.name);
      });
    });
  });
  </code></pre>
  
  <p>When I step through the call stack it seems to be executing correctly (steps inside of the promise callback in the actual js file, then steps into the test promise callback to assert), however the name variable in the test is still coming back as undefined. Any feedback would be appreciated.</p>