Repository navigation
Closed
Labels
area: common/httpIssues related to HTTP and HTTP ClientfeatureLabel used to distinguish feature request from other issuesfeature: in backlogFeature request for which voting has completed and is now in the backlogfeature: under considerationFeature request for which voting has completed and the request is now under considerationstate: has PR
Milestone
Description
I'm submitting a...
[ ] Regression (a behavior that used to work and stopped working in a new release)
[X] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question
Current behavior
Related to #19903
When testing a request that contains query parameters, the test throws 2 errors:
- Expected one matching request for criteria "Match URL: ./assets/mock/modules.json", found none.
- Expected no open requests, found 1: GET ./assets/mock/modules.json
Appending the paramters to the expected URL does not work either.
Expected behavior
Test failure message should show the full path including parameters.
OR
We should be able to specify expected parameters or ignore all.
Minimal reproduction of the problem with instructions
[example.service.spec.ts]
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { Injector } from '@angular/core';
import { async, TestBed } from '@angular/core/testing';
import { ExampleService } from './example.service';
fdescribe('Create a test suite for the Example Service', () => {
let injector: Injector;
let exampleService: ExampleService;
let httpMock: HttpTestingController;
beforeEach(() => {
injector = TestBed.configureTestingModule({
imports:
[HttpClientTestingModule],
providers:
[ExampleService]
});
exampleService = injector.get(ExampleService);
httpMock = injector.get(HttpTestingController);
});
afterEach(() => {
httpMock.verify();
});
it('should not immediately connect to the server', () => {
httpMock.expectNone({});
});
describe('when fetching all stuff', () => {
it('should make a GET request', async(() => {
exampleService.getStuff();
let req = httpMock.expectOne(`./assets/mock/modules.json`);
expect(req.request.method).toEqual('GET');
req.flush([]);
}));
});
});
[example.service.ts]
import { HttpClient, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
@Injectable()
export class ExampleService {
constructor(private http: HttpClient) {
}
getStuff() {
let params = new HttpParams().append('testparam', 'testvalue')
return this.http.get('./assets/mock/modules.json', {params}).toPromise().then(value => {
return value
})
}
}
What is the motivation / use case for changing the behavior?
Currently it is impossible to test http requests in a service which contain query parameters.
Environment
Angular version: 4.4.4
Browser:
- [X] Chrome (desktop) version 61.0.3163.100
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [ ] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX
For Tooling issues:
- Node version: V8.4.0
- Platform: Windows
Others:
Reacted by Jonathan Sharpe, Dominik, Vighnesh K S P, Christoph Kerschenbauer, valentino, Konstantin Polyntsov, Stefan Lindenberg, Bhuwan Prasad Upadhyay, Tharzeez, Alexander Ciesielski and 65 more
Reacted by Trotyl Yu
Reacted by Daniel Martinez and Rui Marques
Metadata
Metadata
Assignees
Labels
area: common/httpIssues related to HTTP and HTTP ClientfeatureLabel used to distinguish feature request from other issuesfeature: in backlogFeature request for which voting has completed and is now in the backlogfeature: under considerationFeature request for which voting has completed and the request is now under considerationstate: has PR

Activity
lenn3k commentedon Oct 27, 2017
The code in this issue seems to work and the actual bug is related to adding params to the http-request.
I will investigate further if this is an actual bug or if there is a different cause.
[-]httpClientTestingModule does not work with toPromise[/-][+]HttpTestingController.expectOne() not working with queryparameters[/+]lenn3k commentedon Oct 30, 2017
Issue related to parameters in the request, not the fact that it returned a promise
textbook commentedon Nov 8, 2017
I just came across the same problem. Fixing the error message would be one improvement, but in my view it would be even better if it matched against
request.urlrather thanrequest.urlWithParams. I'd rather write:than:
not least because it avoids any ordering issues when you have more than one parameter.
A current workaround is to use the request matcher function instead:
25 remaining items
ruimarques commentedon Dec 17, 2019
This is quite annoying, just spent some hours until figuring this should be a bug in expectOne. And here it is.
@textbook work-around works fine, thank you.
denis111 commentedon Jun 4, 2020
I think something is broken here with toPromise() too. I have a service method which makes two requests and returns promise. For the first request it does (this.api.http is HttpClient):
After this line the test fails with that "found none" whether I use request params or not, just:
textbook commentedon Jun 4, 2020
@denis111 it's not obvious to me how you think that relates to this issue. As you say, it's not about whether or not there are query parameters. Putting together a minimal example around the code you've posted, the test passes for me, so you'll need to provide something reproducible.
denis111 commentedon Jun 5, 2020
@textbook Yes, I also did a minimal example and it works, so I compared with real code and in real code I have another await before the request. So this test will fail with this code in a service, for example:
EDIT:
Nevermind. Wrapping the function in fakeAsync and calling tick() worked!
IgnacioMaestro commentedon Mar 5, 2021
I have tried to improve and clarify this solution.
I have used as a parameter of the method expectOne an arrow function that always return true in order not to filter the requests by url. Afterwards I have checked the url, the method and the query parameters sent.
This is my approach:
I think that the function exepctOne could not receive any params and not check any url (it would be an easier code change) in order not to send a strange arrow function.
It could be like this:
ajith4u81 commentedon Apr 26, 2021
I came across the same problem. Solved by subscribing to the method first. As a result of calling the method, the open array in the HttpClientTestingBackend is updated
open [] was always empty when i trigger the tests
Afterward when httpTestingController.expectOne () is getting called, _match helper function, filter based on the match and it will be a success
Pathmatch method

fix(common): include query parameters for open HTTP requests in `verify`
JoostK commentedon Jan 31, 2022
Hi folks, I've opened #44917 as a bug fix to improve the reported error.
I considered some designs to allow matching an URL without its query parameters, but this didn't work out as nicely as I'd hoped to be a viable alternative to using a closure-based matcher.
fix(common): include query parameters for open HTTP requests in `veri…
angular-automatic-lock-bot commentedon Mar 4, 2022
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.