title: Spy behavior-subject and apply .pipe
body: <p>I am running Units test for my component when a try to mock one attribute of type BehaviorSubject on my service, I received this error. "TypeError: Cannot read property 'pipe' of undefined". </p>
  
  <p>I tried to mock one attribute as BehaviorSubject from my service in my component, </p>
  
  <p>component.ts</p>
  
  <pre><code>export class ShowFiltersComponent implements OnInit, OnDestroy {
      private _unsubscribeAll: Subject&lt;any&gt;;
      filterSelected = [];
      constructor(
          private _chr: ChangeDetectorRef,
          private _pmService: PMService
      ) {
          this._unsubscribeAll = new Subject();
      }
  
      ngOnInit() {
          this._pmService.onFilterShowSelected
              .pipe(takeUntil(this._unsubscribeAll))
              .subscribe(res =&gt; {
                  this.filterSelected = res;
                  this._chr.detectChanges();
              });
      }
      ngOnDestroy(): void {
          // Unsubscribe from all subscriptions
          this._unsubscribeAll.next();
          this._unsubscribeAll.complete();
      }
  }
  </code></pre>
  
  <p>pmService.service.ts</p>
  
  <pre><code>export class PMService {
  
      _onFilterShowSelected: BehaviorSubject&lt;any&gt;;
  
      constructor(private _http: HttpClient) {
        this._onFilterShowSelected = new BehaviorSubject({});
      }
  
      get onFilterShowSelected(): BehaviorSubject&lt;any&gt; {
          return this._onFilterShowSelected;
      }
  ....
  }
  </code></pre>
  
  <p>component.spect.ts</p>
  
  <pre><code>describe("ShowFiltersComponent", () =&gt; {
      let component: ShowFiltersComponent;
      let service: PatientManagementService;
      let fixture: ComponentFixture&lt;ShowFiltersComponent&gt;;
      let httpMock: HttpClient;
  
  
      beforeEach(async(() =&gt; {
          const sp = spyOnProperty(
              new PMService(httpMock),
              "onFilterShowSelected",
              "get"
          ).and.returnValue(of({}));
          TestBed.configureTestingModule({
              schemas: [NO_ERRORS_SCHEMA],
  
              declarations: [ShowFiltersComponent],
              imports: [HttpClientModule],
              providers: [
                  {
                      provide: PMService,
                      useValue: sp
                  }
              ]
          }).compileComponents();
      }));
  
      beforeEach(() =&gt; {
          fixture = TestBed.createComponent(ShowFiltersComponent);
          component = fixture.componentInstance;
          fixture.detectChanges();
      });
  
      it("should create", () =&gt; {
          expect(component).toBeTruthy();
      });
  });
  
  </code></pre>