title: Unit test service that returns promise Angularjs Jasmine
body: <p>EDITED per Michal Charemza post. </p>
  
  <p>I have a service that represents angularui modal dialog:</p>
  
  <pre><code>app.factory("dialogFactory", function($modal, $window, $q) {
  
      function confirmDeleteDialog() {
  
      var modalInstance = $modal.open({
          templateUrl: "../application/factories/confirmDeleteDialog.htm",
          controller: function($scope, $modalInstance) {
  
              $scope.ok = function() {
                  $modalInstance.close("true");
              };
  
              $scope.cancel = function() {
                  $modalInstance.dismiss("false");
              };
          }
      });
  
  
      return modalInstance.result.then(function(response) {
          return 'My other success result';
      }, function(response) {
          return $q.reject('My other failure reason');
      });
  
  };
  
      return {
          confirmDeleteDialog: confirmDeleteDialog
      };
  
  });
  </code></pre>
  
  <p>On calling the delete method if the user has clicked Ok from the dialog <code>requestNotificationChannel.deleteMessage(id)</code> is executed.</p>
  
  <pre><code>$scope.deleteMessage = function(id) {
          var result = dialogFactory.confirmDeleteDialog();
  
          result.then(function(response) {
              requestNotificationChannel.deleteMessage(id);
          });
      };
  </code></pre>
  
  <p>The problem is I am not able to unit test this.</p>
  
  <p>This is my test. I have correctly injected the q service but I am not sure what should I return from <code>"confirmDeleteDialog"</code> spy...</p>
  
  <pre><code>describe("has a delete method that should call delete message notification", function() {
              var deferred = $q.defer();
              spyOn(dialogFactory, "confirmDeleteDialog").and.returnValue(deferred.promise);
  
              spyOn(requestNotificationChannel, "deleteMessage");
  
              $scope.deleteMessage(5);
              deferred.resolve();
  
              it("delete message notification is called", function() {
                  expect(requestNotificationChannel.deleteMessage).toHaveBeenCalled();
              });
          });
  </code></pre>
  
  <p>But I am receiving <code>expected spy deleteMessage to have been called</code>. Which means that the <code>result.then</code>... part is not executed. What am I missing ?</p>