fix(common): http/testing expectOne lists the received requests if no matches (#27005)

Fixes #18013

Previously it was hard to debug an `expectOne` if the request had no match, as the error message was:

    Expected one matching request for criteria "Match URL: /some-url?query=hello", found none.

This commit adds a bit more info to the error, by listing the actual requests received:

    Expected one matching request for criteria "Match URL: /some-url?query=hello", found none. Requests received are: POST /some-url?query=world.

PR Close #27005
This commit is contained in:
cexbrayat 2018-11-08 16:25:29 +01:00 committed by Misko Hevery
parent a622a281d3
commit 115b7e42c6
2 changed files with 18 additions and 3 deletions

View File

@ -90,7 +90,19 @@ export class HttpClientTestingBackend implements HttpBackend, HttpTestingControl
`Expected one matching request for criteria "${description}", found ${matches.length} requests.`); `Expected one matching request for criteria "${description}", found ${matches.length} requests.`);
} }
if (matches.length === 0) { if (matches.length === 0) {
throw new Error(`Expected one matching request for criteria "${description}", found none.`); let message = `Expected one matching request for criteria "${description}", found none.`;
if (this.open.length > 0) {
// Show the methods and URLs of open requests in the error, for convenience.
const requests = this.open
.map(testReq => {
const url = testReq.request.urlWithParams;
const method = testReq.request.method;
return `${method} ${url}`;
})
.join(', ');
message += ` Requests received are: ${requests}.`;
}
throw new Error(message);
} }
return matches[0]; return matches[0];
} }

View File

@ -36,7 +36,9 @@ describe('HttpClient TestRequest', () => {
fail(); fail();
} catch (error) { } catch (error) {
expect(error.message) expect(error.message)
.toBe('Expected one matching request for criteria "Match URL: /some-url", found none.'); .toBe(
'Expected one matching request for criteria "Match URL: /some-url", found none.' +
' Requests received are: GET /some-other-url.');
} }
}); });
@ -55,7 +57,8 @@ describe('HttpClient TestRequest', () => {
} catch (error) { } catch (error) {
expect(error.message) expect(error.message)
.toBe( .toBe(
'Expected one matching request for criteria "Match URL: /some-url?query=world", found none.'); 'Expected one matching request for criteria "Match URL: /some-url?query=world", found none.' +
' Requests received are: GET /some-url?query=hello.');
} }
}); });
}); });