title: Unit Testing with Router.events Angular 9
body: <p>I am new to Angular and to unit testing in general and now I can't find a way to test the code in this service below. This is responsable for the creation of a breadcrumb menu. In the Routing Module I have added for every route --&gt; data:{breadcrumb:'custom name'}.</p>
  <p>Here is the service code:</p>
  <pre><code>import {Injectable} from '@angular/core';
  import {BehaviorSubject} from 'rxjs';
  import {IBreadcrumbs} from '../breadcrumb/ibreadcrumbs';
  import {ActivatedRouteSnapshot, Data, NavigationEnd, Router} from '@angular/router';
  import {filter} from 'rxjs/operators';
  
  
  @Injectable({
    providedIn: 'root'
  })
  export class BreadcrumbService {
  
    /// Subject emitting the breadcrumb hierarchy
    private readonly breadcrumbsSub$ = new BehaviorSubject&lt;IBreadcrumbs[]&gt;([]);
  
    // Observable exposing the breadcrumb hierarchy
    readonly breadcrumbs$ = this.breadcrumbsSub$.asObservable();
  
    constructor(private router: Router) {
      this.router.events
        .pipe(
          // Filter the NavigationEnd events as the breadcrumb is updated only when the route reaches its end
          filter((event) =&gt; event instanceof NavigationEnd)
        )
        .subscribe((event) =&gt; {
          // Construct the breadcrumb hierarchy
          const root = this.router.routerState.snapshot.root;
          const breadcrumbs: IBreadcrumbs[] = [];
  
  
          this.addBreadcrumb(root, breadcrumbs);
          // Emit the new hierarchy
          this.breadcrumbsSub$.next(breadcrumbs);
        });
    }
  
  
    private addBreadcrumb(
      route: ActivatedRouteSnapshot | null,
      breadcrumbs: IBreadcrumbs[]
    ) {
      if (route) {
        // Add an element for the current route part
        if (route.data.breadcrumb) {
          const breadcrumb = {
            label: this.getLabel(route.data),
            url: route.url.toString()
          };
          breadcrumbs.push(breadcrumb);
        }
        // Add another element for the next route part
        this.addBreadcrumb(route.firstChild, breadcrumbs);
      }
    }
  
    private getLabel(data: Data) {
      // The breadcrumb can be defined as a static string or as a function to construct the breadcrumb element out of the route data
      return typeof data.breadcrumb === 'function'
        ? data.breadcrumb(data)
        : data.breadcrumb;
    }
  
  
  }
  
  </code></pre>
  <p>And here is my spec.ts file</p>
  <pre><code>import {TestBed} from '@angular/core/testing';
  
  import {BreadcrumbService} from './breadcrumb.service';
  import {RouterTestingModule} from '@angular/router/testing';
  import {Router, RouterEvent} from '@angular/router';
  import {ReplaySubject} from 'rxjs';
  
  
  fdescribe('BreadcrumbService', () =&gt; {
    let service: BreadcrumbService;
    let routerEventRelaySubject: ReplaySubject&lt;RouterEvent&gt;;
    let routerMock;
    let router: Router;
  
  
    beforeEach(() =&gt; {
      routerEventRelaySubject = new ReplaySubject&lt;RouterEvent&gt;(1);
      routerMock = {
        events: routerEventRelaySubject.asObservable()
      };
      TestBed.configureTestingModule({
        imports: [RouterTestingModule],
        providers: [{provide: Router, useValue: routerMock}]
      });
      service = TestBed.inject(BreadcrumbService);
    });
  
    it('should be created', () =&gt; {
      expect(service).toBeTruthy();
    });
  
  });
  </code></pre>
  <p>I need a coverage of 80% minimum, please help me achieve this.
  Thank you in advance.</p>