diff --git a/packages/common/http/testing/src/backend.ts b/packages/common/http/testing/src/backend.ts index b157027e4fb0a..06fb2d8305d5a 100644 --- a/packages/common/http/testing/src/backend.ts +++ b/packages/common/http/testing/src/backend.ts @@ -95,13 +95,7 @@ export class HttpClientTestingBackend implements HttpBackend, HttpTestingControl 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(', '); + const requests = this.open.map(describeRequest).join(', '); message += ` Requests received are: ${requests}.`; } throw new Error(message); @@ -135,12 +129,7 @@ export class HttpClientTestingBackend implements HttpBackend, HttpTestingControl } if (open.length > 0) { // Show the methods and URLs of open requests in the error, for convenience. - const requests = open.map(testReq => { - const url = testReq.request.urlWithParams.split('?')[0]; - const method = testReq.request.method; - return `${method} ${url}`; - }) - .join(', '); + const requests = open.map(describeRequest).join(', '); throw new Error(`Expected no open requests, found ${open.length}: ${requests}`); } } @@ -158,3 +147,9 @@ export class HttpClientTestingBackend implements HttpBackend, HttpTestingControl } } } + +function describeRequest(testRequest: TestRequest): string { + const url = testRequest.request.urlWithParams; + const method = testRequest.request.method; + return `${method} ${url}`; +} diff --git a/packages/common/http/testing/test/request_spec.ts b/packages/common/http/testing/test/request_spec.ts index 4c468297bd2f6..3c6b3f0c4f033 100644 --- a/packages/common/http/testing/test/request_spec.ts +++ b/packages/common/http/testing/test/request_spec.ts @@ -91,4 +91,22 @@ describe('HttpClient TestRequest', () => { ' Requests received are: GET /some-other-url?query=world, POST /and-another-url.'); } }); + + it('throws if there are open requests when verify is called', () => { + const mock = new HttpClientTestingBackend(); + const client = new HttpClient(mock); + + client.get('/some-other-url?query=world').subscribe(); + client.post('/and-another-url', {}).subscribe(); + + try { + mock.verify(); + fail(); + } catch (error) { + expect(error.message) + .toBe( + 'Expected no open requests, found 2:' + + ' GET /some-other-url?query=world, POST /and-another-url'); + } + }); });