I would like to unit test an Angular 10 component. In the component's ngOnInit method, this code appears:
this.subscriptions.push(
this.myService.getDataSubject.subscribe((results) => {
this.data = results;
}
);
Also, in the unit test
beforeEach(async(() => {
TestBed.configureTestingModule({
providers: [MyService]
}).compileComponents();
})
My unit test includes
beforeEach(() => {
spyOnProperty(myService, 'getDataSubject', 'get').and.returnValue(of(mockData));
}
In the service itself
private dataSubject: BehaviorSubject<any> = new BehaviorSubject({});
getDataSubject = this.dataSubject.asObservable();
The unit test fails. I've tried multiple variations but cannot get the mockData value to be returned. When writing a unit test for a component that subscribes to a property tied to a BehaviorSubject, what am I supposed to do?