MockConnection

Experimental Class

Class Overview

1
2
3
4
5
6
7
8
9
10
11
class MockConnection {
  constructor(req: Request)
   
   
  readyState : ReadyState
  request : Request
  response : ReplaySubject<Response>
  mockRespond(res: Response)
  mockDownload(res: Response)
  mockError(err?: Error)
}

Class Description

Mock Connection to represent a Connection for tests.

Constructor

constructor(req: Request)

Class Details

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

Request instance used to create the connection.

response : ReplaySubject<Response>

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

mockRespond(res: Response)

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

1
2
3
4
5
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'
mockDownload(res: Response)

Not yet implemented!

Sends the provided Response to the downloadObserver of the Request associated with this connection.

mockError(err?: Error)

Emits the provided error object as an error to the Response EventEmitter returned from Http.

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

exported from @angular/http/testing/index, defined in @angular/http/testing/mock_backend.ts

doc_Angular
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.