Unit testing with private service injected using jasmine angular2 : <p>I have a problem trying to unit test an angular service. I want to verify that this service is properly calling another service that is injected into it.</p>

<p>Lets say I have this ServiceToTest that injects ServiceInjected:</p>

<p>ServiceToTest .service.ts</p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-html lang-html prettyprint-override"><code>@Injectable()
export class ServiceToTest  {
    constructor(private _si: ServiceInjected) {}
    public init() {
      this._si.configure();
    }

}</code></pre>
</div>
</div>
</p>

<p>ServiceInjected.service.ts
<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-html lang-html prettyprint-override"><code>@Injectable()
export class ServiceInjected {
    constructor() {}
    public configure() {
    /*Some actions*/
    }

}</code></pre>
</div>
</div>
</p>

<p>With these services, now I write my unit test: </p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-html lang-html prettyprint-override"><code>const serviceInjectedStub = {
  configure(): void {}
}


describe('ServiceToTest service Test', () =&gt; {
  beforeEach(() =&gt; {
    TestBed.configureTestingModule({
      providers: [ServiceToTest ,
        { provide: ServiceInjected, useValue: serviceInjectedStub }]
    });
  });
  
  it('should be initialize the service injected', inject([ServiceToTest],
    (tService: ServiceToTest) =&gt; {
      spyOn(serviceInjectedStub, 'configure');
      tService.init();
      expect(serviceInjectedStub.configure).toHaveBeenCalled();
    }));</code></pre>
</div>
</div>
</p>

<p>I expected my test to be positive, however I receive the following error:</p>

<p>Expected spy configure to have been called.</p>

<p>On the other hand, it works OK if I set the injected service public in this way:</p>

<pre><code>private _si: ServiceInjected by public si: ServiceInjected
</code></pre>
