title: Angular 7 - Unit test for an observable with subscribe
body: <p>Hi I am trying to write angular code for a component with an observable but I can't test the subscribe function. How Should I access the subscribe function? Any help would be appreciated. Thank you.</p>
  
  <p><strong>This is my component with the observable:</strong></p>
  
  <pre><code>  ngOnInit(): void {
  
      if (this.authService.getUser() !== null || this.authService.getUser() !== undefined) {
        console.log('getUser');
        this.userService.userDetails = this.authService.getUser();
      }
  
      if (this.environmentName === 'QA' || this.environmentName === 'LOCAL' || this.environmentName === 'QA-STAGING') {
        console.log('environmentName');
        this.authService.acquireTokenSilent(['api://012fdc3a-c966-4312-9b5c-301f097c1803/server']);
      } else {
        this.authService.acquireTokenSilent(['api://58a80bb5-906b-4ec0-9b41-7a78a07125af/server']);
      }
  
      this.subscription.add(
        this.broadcastService.subscribe('msal:acquireTokenSuccess', (payload) =&gt; {
          // do something here
          console.log('acquire token success ' + JSON.stringify(payload));
  
          this.roleService.checkServerEventReviewers().subscribe(res =&gt; {
            this.userService.userDetails.role = res ? 'Data Steward' : 'Mosaic Consumer';
            if (this.isLoggedIn !== true) {
              const redirectUri = sessionStorage.getItem('redirectUri');
              if (redirectUri !== undefined || redirectUri !== null) {
                this.router.navigateByUrl(redirectUri);
              }
            }
            this.isLoggedIn = true;
  };
  </code></pre>
  
  <p><strong>This is my spec file that I am trying:</strong></p>
  
  <pre><code>describe('AppComponent', () =&gt; {
    beforeEach(() =&gt; {
      let subscription: Subscription = new Subscription();
      TestBed.configureTestingModule({
        imports: [RouterTestingModule],
        declarations: [AppComponent],
        providers: [WindowService, RoleService, HttpClient, HttpHandler, BroadcastService, MsalService,
          {
            provide: MSAL_CONFIG,  // MsalService needs config, this provides it.
            useFactory: () =&gt; ({   // Note this is an arrow fn that returns the config object
              redirectUri: window.location.origin + '/',
              clientID: mockData.clientID,
            }),
          }],
        schemas: [CUSTOM_ELEMENTS_SCHEMA]
      }).compileComponents();
    });
  
    describe(':', () =&gt; {
      function setup() {
        const fixture = TestBed.createComponent(AppComponent);
        const app = fixture.debugElement.componentInstance;
        const compiled = fixture.debugElement.nativeElement;
        return {fixture, app, compiled};
      }
  
      it('Init with QA environment', () =&gt; {
        const {app} = setup();
        spyOn(app.authService, 'getUser').and.returnValue(mockData.userDetails);
        spyOn(app.authService, 'acquireTokenSilent').and.returnValue('msal:acquireTokenSuccess');
        app.ngOnInit();
        app.subscription.add(
          app.broadcastService.subscribe('msal:acquireTokenSuccess', () =&gt; {
          // do something here
          });
      );
  </code></pre>