I have a component that depends on SearchAttributeService. There is a method called searchAttributeValidation service that returns a Subject. I am subscribing to that subject like this:
attributeSearchValidation(): void {
this._searchAttributeService.searchAttributeValidation.subscribe((value: string) => {
if (value === this._searchAttributeService.emptyAttributeSearchData) {
this.searchAttributeIndicator = true;
} else if (value === this._searchAttributeService.clearEmptyAttributeSearchDataValidation) {
this.searchAttributeIndicator = false;
});
}
get searchAttributeValidation(): Subject<string> {
return this.searchAttributeValidationSubject;
}
I am trying to test this method like this:
searchAttributeService = TestBed.get(SearchAttributeService);
let spy = spyOn(searchAttributeService, 'searchAttributeValidation').and.returnValue(of('EMPTY-DATA'));
component.attributeSearchValidation();
expect(spy).toHaveBeenCalled();
expect(component.searchAttributeIndicator).toBeTruthy();
It does not call the searchAttributeValidation method at all during the test. I even provided the service TestBed.configureTestingModule({
The test fails with the message: Expected spy searchAttributeValidation to have been called.
Where am I going wrong?? Spent a whole day on it, but, couldn't figure out the issue?
spyOn is for methods but you are accessing a property, if it was a method you would be accessing it like
this._searchAttributeService.searchAttributeValidation().subscribe
with the call () paramaters.
If it is a getter property then use spyOnProperty or if it is an instance property then reassign it.
searchAttributeService.searchAttributeValidation = of('EMPTY-DATA');
and get rid of the expect to have been called.