title: Testing injected service that is a constructor function
body: <p>Say I have a service like this, where a car gets an engine service injected, which is a constructor function:</p>
  
  <pre><code>angular.module('car', ['engine']).factory('carCreator', function( engine ) {
  
      var carCreator = function( settings ) {
  
          var engineInstance = engine( settings );
          engineInstance.setMiles( settings.engine.miles );
  
          return {
              brand: settings.brand;
              engine: engineInstance;
          }
      };
  
      return carCreator;
  });
  </code></pre>
  
  <p>How do I test both lines in the initialization logic:</p>
  
  <pre><code>var engineInstance = engine( settings );
  engineInstance.setMiles( settings.engine.miles )
  </code></pre>
  
  <p>1: That <code>engine</code> is called with <code>settings</code></p>
  
  <p>2: That <code>engineInstance.setMiles</code> is called with <code>settings.engine.miles</code></p>
  
  <p>This is what I'm doing right now, but with no luck:</p>
  
  <pre><code>describe('initialization', function() {
  
      var carCreator;
      var settings = {
          brand: 'Ford',
          engine: {
              miles: 12000
          }
      };
  
      var mockEngineInstance = {
          setMiles: function() {}
      };
  
      window.mockEngineCreator = function() {
          return mockEngineInstance;
      }
  
      beforeEach(module('car', function($provide) {
          $provide.value('engine', mockEngineCreator );
      }));
  
      beforeEach(inject(function(_carCreator_) {
          carCreator = _carCreator_;
      }));
  
      it('should init text object correctly on initialization', function() {
  
          spyOn(window, 'monkEngineCreator');
          spyOn(mockEngineInstance, 'setMiles');
          carCreator( settings );
  
          expect(window.mockEngineCreator).toHaveBeenCalledWith( settings );
          expect(mockEngineInstance.setMiles).toHaveBeenCalledWith( settings.engine.miles );
      });
  });
  </code></pre>
  
  <p>but this test fails, saying that <code>window.mockEngineCreator</code> never was called. It seems that $provide creates a new copy of the function passed in, instead of keeping a reference to it. So, does anyone know how to setup a test that can test this correctly?</p>