title: Getting "$digest already in progress" in async test with Jasmine 2.0
body: <p>I know that calling <code>$digest</code> or <code>$apply</code> manually during a digest cycle will cause a "$digest already in progress" error but I have no idea why I am getting it here.</p>
  
  <p>This is a unit test for a service that wraps <code>$http</code>, the service is simple enough, it just prevents making duplicate calls to the server while ensuring that code that attempts to do the calls still gets the data it expected.</p>
  
  <pre><code>angular.module('services')
      .factory('httpService', ['$http', function($http) {
  
          var pendingCalls = {};
  
          var createKey = function(url, data, method) {
              return method + url + JSON.stringify(data);
          };
  
          var send = function(url, data, method) {
              var key = createKey(url, data, method);
              if (pendingCalls[key]) {
                  return pendingCalls[key];
              }
              var promise = $http({
                  method: method,
                  url: url,
                  data: data
              });
              pendingCalls[key] = promise;
              promise.then(function() {
                  delete pendingCalls[key];
              });
              return promise;
          };
  
          return {
              post: function(url, data) {
                  return send(url, data, 'POST');
              },
              get: function(url, data) {
                  return send(url, data, 'GET');
              },
              _delete: function(url, data) {
                  return send(url, data, 'DELETE');
              }
          };
      }]);
  </code></pre>
  
  <p>The unit-test is also pretty straight forward, it uses <code>$httpBackend</code> to expect the request.</p>
  
  <pre><code>it('does GET requests', function(done) {
      $httpBackend.expectGET('/some/random/url').respond('The response');
  
      service.get('/some/random/url').then(function(result) {
          expect(result.data).toEqual('The response');
          done();
      });
      $httpBackend.flush();
  });
  </code></pre>
  
  <p>This blows up as sone as <code>done()</code> gets called with a "$digest already in progress" error. I've no idea why. I can solve this by wrapping <code>done()</code> in a timeout like this</p>
  
  <pre><code>setTimeout(function() { done() }, 1);
  </code></pre>
  
  <p>That means <code>done()</code> will get queued up and run after the $digest is done but while that solves my problem I want to know </p>
  
  <ul>
  <li>Why is Angular in a digest-cycle in the first place?</li>
  <li>Why does calling <code>done()</code> trigger this error?</li>
  </ul>
  
  <p>I had the exact same test running green with Jasmine 1.3, this only happened after I upgraded to Jasmine 2.0 and rewrote the test to use the new async-syntax.</p>