title: Angular service constructor blocking unit tests
body: <p>I'm facing the problem with writing unit tests for service which has some action in constructor. It is blocking my test and I'm not sure how to deal with it :(</p>
  
  <p>This is service class:</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-js lang-js prettyprint-override"><code>import { Injectable } from '@angular/core';
  
  import { Storage } from '@dvs-angular/storage';
  
  const COOKIE_STORAGE_KEY = 'cookiePanelDisplayed';
  
  @Injectable()
  export class CookiePanelService {
    public panelVisible: boolean = true;
  
    constructor(
      private storage: Storage
    ) {
      this.panelVisible = !this.storage.has(COOKIE_STORAGE_KEY);
    }
  
    public close(): void {
      this.panelVisible = false;
  
      if (this.storage.has(COOKIE_STORAGE_KEY)) {
        return;
      }
  
      this.storage.set(COOKIE_STORAGE_KEY, true);
    }
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>and this is how my tests look like</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-js lang-js prettyprint-override"><code>import { TestBed, inject } from '@angular/core/testing';
  
  import { CookiePanelService } from './cookie-panel.service';
  import { Storage } from '@dvs-angular/storage';
  
  const storageMock = {
    has: jasmine.createSpy()
  };
  
  const COOKIE_STORAGE_KEY = 'cookiePanelDisplayed';
  
  describe('Service: CookiePanelService', () =&gt; {
  
    let cookiePanelService: CookiePanelService;
  
    beforeEach(() =&gt; {
      TestBed.configureTestingModule({
        providers: [
          CookiePanelService,
          {
            provide: Storage,
            useValue: storageMock
          },
        ]
      });
    });
  
    beforeEach(
      inject([CookiePanelService], (service: CookiePanelService) =&gt; {
        cookiePanelService = service;
      })
    );
  
    it('should set panelVisible to false on close', () =&gt; {
      cookiePanelService.panelVisible = true;
      cookiePanelService.close();
  
      expect(cookiePanelService.panelVisible).toBe(false);
    });
  
  });</code></pre>
  </div>
  </div>
  </p>
  
  <p>What I'm getting in console is:
  <code>TypeError: this.storage.has is not a function</code></p>
  
  <p>May you please advice how to deal with constructor and how to avoid this kind of situations.</p>