title: How do I mock a service that returns promise in AngularJS Jasmine unit test?
body: <p>I have <code>myService</code> that uses <code>myOtherService</code>, which makes a remote call, returning promise:</p>
  
  <pre><code>angular.module('app.myService', ['app.myOtherService'])
    .factory('myService', [
      myOtherService,
      function(myOtherService) {
        function makeRemoteCall() {
          return myOtherService.makeRemoteCallReturningPromise();
        }
  
        return {
          makeRemoteCall: makeRemoteCall
        };      
      }
    ])
  </code></pre>
  
  <p>To make a unit test for <code>myService</code> I need to mock <code>myOtherService</code>, such that its <code>makeRemoteCallReturningPromise</code> method returns a promise. This is how I do it:</p>
  
  <pre><code>describe('Testing remote call returning promise', function() {
    var myService;
    var myOtherServiceMock = {};
  
    beforeEach(module('app.myService'));
  
    // I have to inject mock when calling module(),
    // and module() should come before any inject()
    beforeEach(module(function ($provide) {
      $provide.value('myOtherService', myOtherServiceMock);
    }));
  
    // However, in order to properly construct my mock
    // I need $q, which can give me a promise
    beforeEach(inject(function(_myService_, $q){
      myService = _myService_;
      myOtherServiceMock = {
        makeRemoteCallReturningPromise: function() {
          var deferred = $q.defer();
  
          deferred.resolve('Remote call result');
  
          return deferred.promise;
        }    
      };
    }
  
    // Here the value of myOtherServiceMock is not
    // updated, and it is still {}
    it('can do remote call', inject(function() {
      myService.makeRemoteCall() // Error: makeRemoteCall() is not defined on {}
        .then(function() {
          console.log('Success');
        });    
    }));  
  </code></pre>
  
  <p>As you can see from the above, the definition of my mock depends on <code>$q</code>, which I have to load using <code>inject()</code>. Furthermore, injecting the mock should be happening in <code>module()</code>, which should be coming before <code>inject()</code>. However, the value for the mock is not updated once I change it.</p>
  
  <p>What is the proper way to do this?</p>