Error: Expected one matching request for criteria "Match by function: ", found none
Solution 1
Most likely the URLs are not matching. The expectOne
condition (request.url === `${url}search/users?q=name`)
is likely failing. You can log your mock request URL to see what the actual URL is.
const req = httpMock.expectOne(request => {
console.log("url: ", request.url);
return true;
});
Solution 2
Edit: The issue you described might be caused by an issue which is "silent". My experience follows.
First of all check your service dependencies like AuthenticationService
or such.
The request you create might interact with these dependencies further in the pipeline and might get rejected before it even reaches the Angular's HTTP client!
This way the request won't get registered by HttpTestingController
and you will get the found none in the test.
Solution 3
In my case the tested service had a dependency on some other service, which I mocked. The mocked dependency missed mocked function which was called by the tested service. This error was silently swallowed.
Once the mocked function has been added, it started to work correctly.
![Rajat Singh](https://i.stack.imgur.com/XOqkg.png?s=256&g=1)
Rajat Singh
Updated on June 14, 2022Comments
-
Rajat Singh about 2 years
I have a
userService.spec.ts
file that I want to test.The code goes this way. It calls a get function that calls theADMIN_API_URL
.I tried testing the given file but got an error that is mentioned in the title. My user.service.spec.ts file.describe('Service: User service', () => { let httpMock: HttpTestingController; let userService: UserService; let url: string; beforeEach(() => { TestBed.configureTestingModule({ imports: [HttpClientTestingModule], providers: [ { provide: AUTH_API_URL, useValue: 'https://auth.example.com/api/' }, { provide: SSO_API_URL, useValue: 'https://sso.example.com/auth/api/' }, { provide: WIT_API_PROXY, useValue: 'https://wit.example.com/api/'}, { provide: ADMIN_API_URL, useValue: 'https://admin.example.com/api/'}, { provide: REALM, useValue: 'realm' }, Broadcaster, Logger, AuthenticationService, HttpHandler, UserService ] }); httpMock = TestBed.get(HttpTestingController); url = TestBed.get(ADMIN_API_URL); userService = TestBed.get(UserService); }); it('Get user by user name returns valid user', (done) => { const testUser = [{ 'attributes': { 'fullName': 'name', 'imageURL': '', 'username': 'myUser' }, 'id': 'userId', 'type': 'userType' }]; userService.getUsersByName('name').subscribe((user) => { expect(user[1].id).toEqual('userId'); done(); }); const req = httpMock.expectOne(request => request.method === 'GET' && request.url === `${url}search/users?q=name`); req.flush({data: testUser}); }); });
I tried testing all the things that I had in my mind but still can't find what is wrong with my code.Am I doing any silly mistake?.
-
jasie about 5 yearsThis is not an answer to the problem, you should delete this and move it in a comment. Also, the code is wrong - count the parentheses, and it errors in 'void is not assignable to boolean'.
-
Atif Majeed about 5 yearsCorrected the missed parentheses. The answer is correct. The error occurs when the URL expected by expectOne is not the same as the one used by the service. The code sample is just to give an idea that the questioner can confirm the incoming URL by logging it and then can fix the expected URL in the criteria (request.url ===
${url}search/users?q=name
). -
Richard over 3 yearsThis doesn't work for me on Angular 10
No overload matches this call