如何使用 Jasmine& Angular 模拟嵌套函数

How can I mock nested function with Jasmine& Angular

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

}

我有一个服务作为依赖注入。我想模拟它进行测试。 我知道如何模拟服务和一些方法

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

但是我怎样才能到达嵌套函数?(searchServiceMock.pagesOutput.subscribe?)

由于您要订阅该值,如果您 return 一个可观察对象,订阅将起作用。为了模拟一个 observable,我使用 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 */))