如何对嵌套的 HTTP 调用进行单元测试
How to unit test nested HTTP Calls
我尝试对以下函数进行单元测试:
async fetchGreatHouseByName(name: string) {
const [house] = await this.httpGetHouseByName(name);
const currentLord = house.currentLord ? house.currentLord : '957';
const result: any = await this.httpGetLord(currentLord);
const character = Characters.default.find(char => char.characterName === result.name);
return {...house, character};
}
此函数内部有多个 HTTP GET 调用。我知道如何测试它们,但是我如何处理嵌套的 HTTP 调用,每个调用都是一个 Promise。
我得到了什么:
it('should get house from http', done => {
const httpMock: HttpTestingController = TestBed.inject(HttpTestingController);
const mockResponse = {
name: 'House One',
currentLord: 'Some Lord'
};
service.fetchGreatHouseByName('House One').then((house: House) => {
expect(house).toBeTruthy();
console.log(house);
});
const mockRequest = httpMock.expectOne(
'https://www.anapioficeandfire.com/api/houses?name=House%20Lannister%20of%20Casterly%20Rock'
);
mockRequest.flush(mockResponse);
});
所以 httpGetHouseByName
通常 returns 类似于 mockRequest
。 httpGetLord
和包含 name
等属性的对象。我需要为这个 HTTP 调用再添加一个 mock 吗?如果需要,我该如何使用它?
我认为调用该函数应该将两个 HTTP 调用放入队列中。尝试依次刷新这两个调用并将您的断言放入 .then
块中。
it('should get house from http', done => {
const httpMock: HttpTestingController = TestBed.inject(HttpTestingController);
const mockResponse = {
name: 'House One',
currentLord: 'Some Lord'
};
service.fetchGreatHouseByName('House One').then((house: House) => {
expect(house).toBeTruthy();
console.log(house);
// your other assertions here
});
const mockRequest = httpMock.expectOne(
'https://www.anapioficeandfire.com/api/houses?name=House%20Lannister%20of%20Casterly%20Rock'
);
mockRequest.flush(mockResponse);
const response = {
// mock response of httpGetLord here
};
const request = httpMock.expectOne(/* url of httpGetLord*/);
request.flush(response);
});
我尝试对以下函数进行单元测试:
async fetchGreatHouseByName(name: string) {
const [house] = await this.httpGetHouseByName(name);
const currentLord = house.currentLord ? house.currentLord : '957';
const result: any = await this.httpGetLord(currentLord);
const character = Characters.default.find(char => char.characterName === result.name);
return {...house, character};
}
此函数内部有多个 HTTP GET 调用。我知道如何测试它们,但是我如何处理嵌套的 HTTP 调用,每个调用都是一个 Promise。
我得到了什么:
it('should get house from http', done => {
const httpMock: HttpTestingController = TestBed.inject(HttpTestingController);
const mockResponse = {
name: 'House One',
currentLord: 'Some Lord'
};
service.fetchGreatHouseByName('House One').then((house: House) => {
expect(house).toBeTruthy();
console.log(house);
});
const mockRequest = httpMock.expectOne(
'https://www.anapioficeandfire.com/api/houses?name=House%20Lannister%20of%20Casterly%20Rock'
);
mockRequest.flush(mockResponse);
});
所以 httpGetHouseByName
通常 returns 类似于 mockRequest
。 httpGetLord
和包含 name
等属性的对象。我需要为这个 HTTP 调用再添加一个 mock 吗?如果需要,我该如何使用它?
我认为调用该函数应该将两个 HTTP 调用放入队列中。尝试依次刷新这两个调用并将您的断言放入 .then
块中。
it('should get house from http', done => {
const httpMock: HttpTestingController = TestBed.inject(HttpTestingController);
const mockResponse = {
name: 'House One',
currentLord: 'Some Lord'
};
service.fetchGreatHouseByName('House One').then((house: House) => {
expect(house).toBeTruthy();
console.log(house);
// your other assertions here
});
const mockRequest = httpMock.expectOne(
'https://www.anapioficeandfire.com/api/houses?name=House%20Lannister%20of%20Casterly%20Rock'
);
mockRequest.flush(mockResponse);
const response = {
// mock response of httpGetLord here
};
const request = httpMock.expectOne(/* url of httpGetLord*/);
request.flush(response);
});