MockConnection
From Get docs
< @angular/http | testingAngular/docs/7/api/http/testing/mockconnection
MockConnection
class
deprecated
Mock Connection to represent a Connection for tests.
Deprecated: 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
constructor(req: Request)Parameters
|
Properties
Property | Description |
---|---|
readyState: ReadyState
|
Describes the state of the connection, based on XMLHttpRequest.readyState , but with additional states. For example, state 5 indicates an aborted connection.
|
request: Request
|
|
response: ReplaySubject<Response>
|
|
Methods
mockRespond() | |||
---|---|---|---|
Sends a mock response to the connection. This response is the value that is emitted to the EventEmitter returned by Http. | |||
mockRespond(res: Response)Parameters
|
mockDownload() | |||
---|---|---|---|
Not yet implemented! | |||
mockDownload(res: Response)Parameters
| |||
Sends the provided Response to the downloadObserver of the Request associated with this connection.
|
mockError() | |||
---|---|---|---|
Emits the provided error object as an error to the Response EventEmitter returned from Http. | |||
mockError(err?: Error)Parameters
|
Usage notes
Example of mockRespond()
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'
Example of mockError()
var connection; backend.connections.subscribe(c => connection = c); http.request('data.json').subscribe(res => res, err => console.log(err))); connection.mockError(new Error('error'));
© 2010–2019 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
https://v7.angular.io/api/http/testing/MockConnection