Я хотел бы больше узнать о HttpParams. У меня есть простая служба данных в моем приложении Angular с двумя функциями - getWithHttpParams, getWithURLParams.
@Injectable({
providedIn: "root"
})
export class DataService2 {
constructor(private http: HttpClient) { }
getWithHttpParams(apiUrl:string, ID: string) {
const reqParams = new HttpParams().set('ID', ID);
return this.http.get(Configuration.apiPath + apiUrl, {params: reqParams}).pipe(
map((res: any) => {
return res;
})
);
}
getWithURLParams(apiUrl:string, ID: string) {
return this.http.get(Configuration.apiPath + apiUrl + "?ID=" + ID).pipe(
map((res: any) => {
return res;
})
);
}
}
Я пишу модульный тест для тестирования этой службы.
it('should make a GET request with HttpParams', () => {
service.getWithHttpParams('test', '123').subscribe(data => {
expect(data).toEqual('test data');
});
const req = httpTestingController.expectOne(Configuration.apiPath + 'test?ID=123')
expect(req.request.method).toEqual('GET');
expect(req.request.params.get('ID')).toEqual('123');
req.flush('test data');
});
it('should make a GET request with URL Params', () => {
service.getWithURLParams('test', '123').subscribe(data => {
expect(data).toEqual('test data');
});
const req = httpTestingController.expectOne(Configuration.apiPath + 'test?ID=123')
expect(req.request.method).toEqual('GET');
expect(req.request.params.get('ID')).toEqual('123');
req.flush('test data');
});
Итак, 1-й тест, использующий getWithHttpParams, проходит, но 2-й тест, использующий getWithURLParams, не проходит. В чем причина?