title: Injecting a mock into an AngularJS service
body: <p>I have an AngularJS service written and I would like to unit test it.</p>
  
  <pre><code>angular.module('myServiceProvider', ['fooServiceProvider', 'barServiceProvider']).
      factory('myService', function ($http, fooService, barService) {
  
      this.something = function() {
          // Do something with the injected services
      };
  
      return this;
  });
  </code></pre>
  
  <p>My app.js file has these registered:</p>
  
  <pre><code>angular
  .module('myApp', ['fooServiceProvider','barServiceProvider','myServiceProvider']
  )
  </code></pre>
  
  <p>I can test the DI is working as such:</p>
  
  <pre><code>describe("Using the DI framework", function() {
      beforeEach(module('fooServiceProvider'));
      beforeEach(module('barServiceProvider'));
      beforeEach(module('myServiceProvder'));
  
      var service;
  
      beforeEach(inject(function(fooService, barService, myService) {
          service=myService;
      }));
  
      it("can be instantiated", function() {
          expect(service).not.toBeNull();
      });
  });
  </code></pre>
  
  <p>This proved that the service can be created by the DI framework, however next I want to unit test the service, which means mocking out the injected objects.</p>
  
  <p>How do I go about doing this?</p>
  
  <p>I've tried putting my mock objects in the module, e.g.</p>
  
  <pre><code>beforeEach(module(mockNavigationService));
  </code></pre>
  
  <p>and rewriting the service definition as:</p>
  
  <pre><code>function MyService(http, fooService, barService) {
      this.somthing = function() {
          // Do something with the injected services
      };
  });
  
  angular.module('myServiceProvider', ['fooServiceProvider', 'barServiceProvider']).
      factory('myService', function ($http, fooService, barService) { return new MyService($http, fooService, barService); })
  </code></pre>
  
  <p>But the latter seems to stop the service being created by the DI as all.</p>
  
  <p>Does anybody know how I can mock the injected services for my unit tests?</p>
  
  <p>Thanks</p>
  
  <p>David</p>