1
constructor(private searchService: SearchService, private storageService: StorageService) { 
this.searchService.pagesOutput.subscribe(images => {
  this.images = images;
  this.pages = this.searchService.numberOfPages;
  
})

}

I have a service as a dependency injection. I would like to mock it for test. I know how to mock service and some methods

 searchServiceMock = jasmine.createSpyObj('SearchService', ['pagesOutput']);
 searchServiceMock.pagesOutput.and.returnValue(someValue)

But how can I reach nested functions?(searchServiceMock.pagesOutput.subscribe?)

1 Answer 1

1

Since you're going to be subscribing to the value, if you return an observable, the subscribe will work. To mock an observable, I use of.

import { of } from 'rxjs';
...
searchServiceMock = jasmine.createSpyObj('SearchService', ['pagesOutput']);
searchServiceMock.pagesOutput.and.returnValue(of(/* whatever value you want to be for images in subscribe callback */))
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.