title: AngularJS inject service mock inside service tests
body: <p>I have been trying to test a service to no avail for some time now and was hoping for some help. Here is my situation: </p>
  
  <p>I have a <strong>service</strong> looking a little like this</p>
  
  <pre><code>myModule.factory('myService', ['$rootScope', '$routeParams', '$location', function($rootScope, $routeParams, $location) {
  
    var mySvc = {
      params: {}
    }
  
    // Listen to route changes.
    $rootScope.$on('$routeUpdate', mySvc.updateHandler);
  
    // Update @params when route changes
    mySvc.updateHandler = function(){ ... };
  
    ...
    ...
  
    return mySvc;
  
  }]);
  </code></pre>
  
  <p>And I want to mock the services injected into <code>'myService'</code> before the service gets injected into my tests so I can test the <strong>initialization code</strong> below</p>
  
  <pre><code>  var mySvc = {
      params: {}
    }
  
    // Listen to route changes.
    $rootScope.$on('$routeUpdate', mySvc.updateHandler);
  </code></pre>
  
  <p>I am using Jasmine for tests and mocks. This is what I came up with for now</p>
  
  <pre><code>describe('myService', function(){
    var rootScope, target;
    beforeEach(function(){
      rootScope = jasmine.createSpyObj('rootScope', ['$on']);
  
      module('myModule');
      angular.module('Mocks', []).service('$rootScope', rootScope );
      inject(function(myService){
        target = myService;
      });        
    });
  
    it('should be defined', function(){
      expect(target).toBeDefined();
    });
  
    it('should have an empty list of params', function(){
      expect(target.params).toEqual({});
    });
  
    it('should have called rootScope.$on', function(){
      expect(rootScope.$on).toHaveBeenCalled();
    });
  });
  </code></pre>
  
  <p>This doesn't work though. My rootscope mock is not replacing the original and the  Dependency Injection doc is confusing me more than anything.</p>
  
  <p>Please help</p>