{ "id": "api/common/http/testing/HttpTestingController", "title": "HttpTestingController", "contents": "\n\n
\n
\n
\n \n API > @angular/common > @angular/common/http/testing\n
\n \n
\n \n
\n

HttpTestingControllerlink

\n \n \n \n \n \n
\n \n \n\n
\n \n
\n

Controller to be injected into tests, that allows for mocking and flushing\nof requests.

\n\n \n
\n \n \n \n
\n\nabstract class HttpTestingController {\n abstract match(match: string | RequestMatch | ((req: HttpRequest<any>) => boolean)): TestRequest[]\n abstract expectOne(url: string, description?: string): TestRequest\n abstract expectNone(url: string, description?: string): void\n abstract verify(opts?: { ignoreCancelled?: boolean; }): void\n}\n\n\n \n \n\n
\n\n\n \n\n \n \n \n\n\n\n\n\n\n\n\n\n\n\n\n
\n

Methodslink

\n \n \n\n \n \n \n \n \n \n \n \n \n \n\n \n\n \n \n
\n
\n

\n match()\n \n link

\n \n
\n
\n

Search for requests that match the given parameter, without any expectations.

\n\n
\n
\n \n\n abstract match(match: string | RequestMatch | ((req: HttpRequest<any>) => boolean)): TestRequest[]\n\n \n\n
Parameters
\n \n \n \n \n \n \n \n \n \n
\n \n match\n string | RequestMatch | ((req: HttpRequest) => boolean)\n \n \n
\n\n \n
Returns
\n

TestRequest[]

\n\n \n\n\n \n\n \n
\n
\n\n \n \n\n \n \n \n \n \n \n \n \n \n\n \n \n \n\n \n \n
\n
\n

\n expectOne()\n \n link

\n \n
\n
\n

Expect that a single request has been made which matches the given URL, and return its\nmock.

\n\n
\n
\n \n
\n

3 overloads...

\n \n Show All\n Hide All\n expand_more\n \n
\n
\n
\n \n
\n \n\n abstract expectOne(url: string, description?: string): TestRequest\n\n \n\n
Parameters
\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
\n \n url\n string\n \n \n
\n \n description\n string\n

Optional. Default is undefined.

\n \n
\n\n \n
Returns
\n

TestRequest

\n\n \n\n\n \n\n \n
\n
\n \n \n
Overload #1
\n
\n \n
\n

Expect that a single request has been made which matches the given parameters, and return\nits mock.

\n\n
\n\n abstract expectOne(params: RequestMatch, description?: string): TestRequest\n\n \n\n
Parameters
\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
\n \n params\n RequestMatch\n \n \n
\n \n description\n string\n

Optional. Default is undefined.

\n \n
\n\n \n
Returns
\n

TestRequest

\n\n \n\n\n \n\n \n
\n
\n \n
Overload #2
\n
\n \n
\n

Expect that a single request has been made which matches the given predicate function, and\nreturn its mock.

\n\n
\n\n abstract expectOne(matchFn: (req: HttpRequest<any>) => boolean, description?: string): TestRequest\n\n \n\n
Parameters
\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
\n \n matchFn\n (req: HttpRequest) => boolean\n \n \n
\n \n description\n string\n

Optional. Default is undefined.

\n \n
\n\n \n
Returns
\n

TestRequest

\n\n \n\n\n \n\n \n
\n
\n \n
Overload #3
\n
\n \n
\n

Expect that a single request has been made which matches the given condition, and return\nits mock.

\n\n
\n\n abstract expectOne(match: string | RequestMatch | ((req: HttpRequest<any>) => boolean), description?: string): TestRequest\n\n \n\n
Parameters
\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
\n \n match\n string | RequestMatch | ((req: HttpRequest) => boolean)\n \n \n
\n \n description\n string\n

Optional. Default is undefined.

\n \n
\n\n \n
Returns
\n

TestRequest

\n\n \n\n\n \n\n \n
\n \n \n
\n
\n
\n

If no such request has been made, or more than one such request has been made, fail with an\nerror message including the given request description, if any.

\n\n
\n\n \n \n\n \n \n \n \n \n \n \n \n \n\n \n \n \n\n \n \n
\n
\n

\n expectNone()\n \n link

\n \n
\n
\n

Expect that no requests have been made which match the given URL.

\n\n
\n
\n \n
\n

3 overloads...

\n \n Show All\n Hide All\n expand_more\n \n
\n
\n
\n \n
\n \n\n abstract expectNone(url: string, description?: string): void\n\n \n\n
Parameters
\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
\n \n url\n string\n \n \n
\n \n description\n string\n

Optional. Default is undefined.

\n \n
\n\n \n
Returns
\n

void

\n\n \n\n\n \n\n \n
\n
\n \n \n
Overload #1
\n
\n \n
\n

Expect that no requests have been made which match the given parameters.

\n\n
\n\n abstract expectNone(params: RequestMatch, description?: string): void\n\n \n\n
Parameters
\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
\n \n params\n RequestMatch\n \n \n
\n \n description\n string\n

Optional. Default is undefined.

\n \n
\n\n \n
Returns
\n

void

\n\n \n\n\n \n\n \n
\n
\n \n
Overload #2
\n
\n \n
\n

Expect that no requests have been made which match the given predicate function.

\n\n
\n\n abstract expectNone(matchFn: (req: HttpRequest<any>) => boolean, description?: string): void\n\n \n\n
Parameters
\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
\n \n matchFn\n (req: HttpRequest) => boolean\n \n \n
\n \n description\n string\n

Optional. Default is undefined.

\n \n
\n\n \n
Returns
\n

void

\n\n \n\n\n \n\n \n
\n
\n \n
Overload #3
\n
\n \n
\n

Expect that no requests have been made which match the given condition.

\n\n
\n\n abstract expectNone(match: string | RequestMatch | ((req: HttpRequest<any>) => boolean), description?: string): void\n\n \n\n
Parameters
\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
\n \n match\n string | RequestMatch | ((req: HttpRequest) => boolean)\n \n \n
\n \n description\n string\n

Optional. Default is undefined.

\n \n
\n\n \n
Returns
\n

void

\n\n \n\n\n \n\n \n
\n \n \n
\n
\n
\n

If a matching request has been made, fail with an error message including the given request\ndescription, if any.

\n\n
\n\n \n \n\n \n \n \n \n \n \n \n \n \n \n\n \n \n \n\n \n \n
\n
\n

\n verify()\n \n link

\n \n
\n
\n

Verify that no unmatched requests are outstanding.

\n\n
\n
\n \n\n abstract verify(opts?: { ignoreCancelled?: boolean; }): void\n\n \n\n
Parameters
\n \n \n \n \n \n \n \n \n \n
\n \n opts\n object\n

Optional. Default is undefined.

\n \n
\n\n \n
Returns
\n

void

\n\n \n\n\n \n\n \n
\n
\n

If any requests are outstanding, fail with an error message indicating which requests were not\nhandled.

\n

If ignoreCancelled is not set (the default), verify() will also fail if cancelled requests\nwere not explicitly matched.

\n\n
\n\n \n
\n\n\n\n \n\n\n
\n
\n\n\n" }