How can I verify HTTP query parameters without hardcoding parameter order? #454
Unanswered
grosch-intl
asked this question in
Q&A
Replies: 2 comments 1 reply
-
Spectator delegates the request to Angular. You can check the Angular docs. |
Beta Was this translation helpful? Give feedback.
0 replies
-
@NetanelBasal I came up with the following, which seems to be working. Do you think this would be valuable to add to spectator? public expectOneWithQueryParams(url: string, params: HttpParams, method: HttpMethod): TestRequest {
expect(true).toBe(true); // workaround to avoid `Spec has no expectations` https://github.com/NetanelBasal/spectator/issues/75
const requests = this.controller.match(request => {
if (request.method.toUpperCase() !== method || url !== request.url) {
return false;
}
const expectedKeys = params.keys();
const requestKeys = request.params.keys();
return expectedKeys.length === requestKeys.length
&& expectedKeys.every(key => {
const expectedValues = params.getAll(key);
const requestValues = request.params.getAll(key);
return expectedValues?.length === requestValues?.length
&& expectedValues?.every((value, index) => value === requestValues![index]);
});
});
const matches = requests.length;
if (matches !== 1) {
throw new Error(`Expected one matching request, found ${matches} requests.`);
}
this.controller.verify();
return requests[0];
} |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I'm submitting a...
Current behavior
I have a service call that creates
HttpParams
based on input, and so in my test I want to validate that parameters are being added properly. If I callspectator.expectOne
I have to give the full URL, including the params, but that means my tests have to always know exactly what order the params are being added, which I'd rather not do.Is there some way to "expect" the URL without the query params, and then get the params returned so that I can just look at key/value pairs?
Environment
Beta Was this translation helpful? Give feedback.
All reactions