MockConnection

Mock Connection to represent a Connectionfor tests.

弃用说明

see https://angular.io/guide/http

class MockConnection implements Connection { constructor(req: Request) readyState: ReadyState request: Request response: ReplaySubject<Response> mockRespond(res: Response) mockDownload(res: Response) mockError(err?: Error) }

构造函数

constructor(req: Request)

参数

req

Type: Request.

属性

属性名类型说明
readyState

Describes the state of the connection, based on XMLHttpRequest.readyState, but with additional states. For example, state 5 indicates an aborted connection.

requestRequest

instance used to create the connection.

responseEventEmitter

of Response. Can be subscribed to in order to be notified when a response is available.

方法

Sends a mock response to the connection. This response is the value that is emitted to the EventEmitterreturned by Http.

mockRespond(res: Response)

参数

res

Type: Response.

Example

var connection; backend.connections.subscribe(c => connection = c); http.request('data.json').subscribe(res => console.log(res.text())); connection.mockRespond(new Response(new ResponseOptions({ body: 'fake response' }))); //logs 'fake response'

Not yet implemented!

mockDownload(res: Response)

参数

res

Type: Response.

Sends the provided Responseto the downloadObserver of the Request associated with this connection.

Emits the provided error object as an error to the ResponseEventEmitterreturned from Http.

mockError(err?: Error)

参数

err

Type: Error.

可选. 默认值是 undefined.

Example

var connection; backend.connections.subscribe(c => connection = c); http.request('data.json').subscribe(res => res, err => console.log(err))); connection.mockError(new Error('error'));