如何在链式请求中对 mergeMap 进行单元测试,例如以下服务中的请求:
import { Injectable } from "@angular/core";
import { HttpClient } from "@angular/common/http";
import { mergeMap, delay } from "rxjs/operators";
@Injectable({
providedIn: "root"
})
export class reqService {
constructor(private httpClient: HttpClient) {}
getMultimpleReq() {
return this.httpClient.get(`https://swapi.dev/api/starships/9/`).pipe(
mergeMap(obj => {
console.log("first request", obj);
let filmFromFirstRequest = obj.MGLT;
return this.httpClient
.get(`https://swapi.dev/api/people/${filmFromFirstRequest}/`)
.pipe(delay(1000));
})
);
}
}
这是https://stackblitz.com/edit/angular-chainrequests?devtoolsheight=33&file=src/app/reqservice.ts上的完整应用示例代码