填寫這份《一分鐘調查》,幫我們(開發組)做得更好!去填寫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.