title: Unit-test promise-based code in Angularjs
body: <p>I'm having hard times trying to test promise-based code in Angularjs.</p>
  
  <p>I have the following code in my controller:</p>
  
  <pre><code>    $scope.markAsDone = function(taskId) {
          tasksService.removeAndGetNext(taskId).then(function(nextTask) {
              goTo(nextTask);
          })
      };
  
      function goTo(nextTask) {
          $location.path(...);
      }
  </code></pre>
  
  <p>I'd like to unit-test the following cases:</p>
  
  <ul>
  <li>when <code>markAsDone</code> is called it should call <code>tasksService.removeAndGetNext</code></li>
  <li>when <code>tasksService.removeAndGetNext</code> is done it should change location (invoke <code>goTo</code>)</li>
  </ul>
  
  <p>It seems to me that there is no easy way to test those two cases separately.</p>
  
  <p>What I did to test the first one was:</p>
  
  <pre><code>var noopPromise= {then: function() {}}
  spyOn(tasksService, 'removeAndGetNext').andReturn(noopPromise);
  </code></pre>
  
  <p>Now to test the second case I need to create another fake promise that would be always <code>resolved</code>. It's all quite tedious and it's a lot of boilerplate code.</p>
  
  <p>Is there any other way to test such things? Or does my design smell?</p>