HttpTestingController.expectOne() with queryparameters
11,308
you are passing a parameter. So something like this will work:
const req = httpMock.expectOne(
{ method: 'GET', url:'https://api.github.com/users/blacksonic?user_id=test' });
Author by
samir
Updated on July 06, 2022Comments
-
samir almost 2 years
I am new to angular testing using Jasmine/Karma. I encountered a problem while testing an angular service using
HttpTestingController
. Here is a part of the source code :getProfile(userName: string) { let config = { params: { user_id: "test" } } return this.http .get(`https://api.github.com/users/${userName}`, config); }
when calling the service using
expectOne
ofHttpTestingController
:it('should add an Authorization header', () => { let response; userService.getProfile('blacksonic').subscribe(response => { expect(response).toBeTruthy(); }); const req = httpMock.expectOne({ method: 'GET', url:'https://api.github.com/users/blacksonic' }); });
I get the following error :
Error: Expected one matching request for criteria "Match method: GET, URL: https://api.github.com/users/blacksonic", found none. at HttpClientTestingBackend.expectOne (./node_modules/@angular/common/fesm5/http/testing.js?:301:19) at UserContext.eval (./src/app/Interceptors/Interceptor.spec.ts?:85:28) at ZoneDelegate.invoke (./node_modules/zone.js/dist/zone.js?:387:26) at ProxyZoneSpec.onInvoke (./node_modules/zone.js/dist/zone-testing.js?:287:39) at ZoneDelegate.invoke (./node_modules/zone.js/dist/zone.js?:386:32) at Zone.run (./node_modules/zone.js/dist/zone.js?:137:43) at runInTestZone (./node_modules/zone.js/dist/zone-testing.js?:508:34) at UserContext.eval (./node_modules/zone.js/dist/zone-testing.js?:523:20)
-
samir over 5 yearsi even test this case but it is not working , same error.
-
samir over 5 yearsproblem solved by replacing :
const req = httpMock.expectOne({ method: 'GET', url:'https://api.github.com/users/blacksonic' });
; byconst req = httpMock.expectOne( (req: HttpRequest<any>) => req.urlWithParams == "https://api.github.com/users/blacksonic?user_id=test");