填写这份《一分钟调查》,帮我们(开发组)做得更好!去填写Home

HttpTestingController

控制器将被注入到测试中,从而可以模拟和刷新请求。

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

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

Provided in

方法

搜索与给定参数匹配且不带任何期望语句的请求。

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

      
      abstract match(match: string | RequestMatch | ((req: HttpRequest<any>) => boolean)): TestRequest[]
    
参数
match string | RequestMatch | ((req: HttpRequest) => boolean)
返回值

TestRequest[]

期望发出一个与给定 URL 匹配的单个请求,然后返回其模拟对象。

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

3 个重载形式...

显示所有 隐藏所有 expand_more
      
      abstract expectOne(url: string, description?: string): TestRequest
    
参数
url string
description string
可选. 默认值是 `undefined`.
返回值

TestRequest


Overload #1

期望发出一个与给定参数匹配的单个请求,然后返回其模拟对象。

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

      
      abstract expectOne(params: RequestMatch, description?: string): TestRequest
    
参数
params RequestMatch
description string
可选. 默认值是 `undefined`.
返回值

TestRequest


Overload #2

期望发出一个与给定谓词函数匹配的单个请求,然后返回其模拟对象。

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

      
      abstract expectOne(matchFn: (req: HttpRequest<any>) => boolean, description?: string): TestRequest
    
参数
matchFn (req: HttpRequest) => boolean
description string
可选. 默认值是 `undefined`.
返回值

TestRequest


Overload #3

期望发出一个与给定条件匹配的单个请求,然后返回其模拟对象。

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

      
      abstract expectOne(match: string | RequestMatch | ((req: HttpRequest<any>) => boolean), description?: string): TestRequest
    
参数
match string | RequestMatch | ((req: HttpRequest) => boolean)
description string
可选. 默认值是 `undefined`.
返回值

TestRequest

如果没有发出这样的请求,或者发出过多个这样的请求,则失败,并显示一条错误消息,其中包括给定请求的描述(如果有)。

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

期望没有发出过与给定 URL 匹配的请求。

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

3 个重载形式...

显示所有 隐藏所有 expand_more
      
      abstract expectNone(url: string, description?: string): void
    
参数
url string
description string
可选. 默认值是 `undefined`.
返回值

void


Overload #1

期望没有发出过与给定参数匹配的请求。

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

      
      abstract expectNone(params: RequestMatch, description?: string): void
    
参数
params RequestMatch
description string
可选. 默认值是 `undefined`.
返回值

void


Overload #2

期望没有发出过与给定谓词函数匹配的请求。

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

      
      abstract expectNone(matchFn: (req: HttpRequest<any>) => boolean, description?: string): void
    
参数
matchFn (req: HttpRequest) => boolean
description string
可选. 默认值是 `undefined`.
返回值

void


Overload #3

期望没有发出过与给定条件匹配的请求。

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

      
      abstract expectNone(match: string | RequestMatch | ((req: HttpRequest<any>) => boolean), description?: string): void
    
参数
match string | RequestMatch | ((req: HttpRequest) => boolean)
description string
可选. 默认值是 `undefined`.
返回值

void

如果发出了匹配的请求,则失败并显示一条错误消息,其中包括给定请求的描述(如果有)。

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

验证没有任何未匹配的请求正在等待。

Verify that no unmatched requests are outstanding.

      
      abstract verify(opts?: { ignoreCancelled?: boolean; }): void
    
参数
opts object
可选. 默认值是 `undefined`.
返回值

void

如果有任何未完成的请求,则失败并显示一条错误消息,指出未处理哪些请求。

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

如果未设置过 ignoreCancelled(默认),且未明确匹配已取消的请求,则 verify() 就会失败。

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