Class Overview
class MockBackend { constructor() connections : any connectionsArray : MockConnection[] pendingConnections : any verifyNoPendingRequests() resolveAllConnections() createConnection(req: Request) : MockConnection }
Class Description
A mock backend for testing the Http
service.
This class can be injected in tests, and should be used to override providers to other backends, such as XHRBackend
.
Example
import {BaseRequestOptions, Http} from '@angular/http'; import {MockBackend} from '@angular/http/testing'; it('should get some data', inject([AsyncTestCompleter], (async) => { var connection; var injector = Injector.resolveAndCreate([ MockBackend, {provide: Http, useFactory: (backend, options) => { return new Http(backend, options); }, deps: [MockBackend, BaseRequestOptions]}]); var http = injector.get(Http); var backend = injector.get(MockBackend); //Assign any newly-created connection to local variable backend.connections.subscribe(c => connection = c); http.request('data.json').subscribe((res) => { expect(res.text()).toBe('awesome'); async.done(); }); connection.mockRespond(new Response('awesome')); }));
This method only exists in the mock implementation, not in real Backends.
Annotations
@Injectable()
Constructor
constructor()
Class Details
connections : any
EventEmitter
of MockConnection
instances that have been created by this backend. Can be subscribed to in order to respond to connections.
import {Http, BaseRequestOptions, Response} from '@angular/http'; import {MockBackend} from '@angular/http/testing'; import {Injector, provide} from '@angular/core'; it('should get a response', () => { var connection; //this will be set when a new connection is emitted from the backend. var text; //this will be set from mock response var injector = Injector.resolveAndCreate([ MockBackend, {provide: Http, useFactory: (backend, options) => { return new Http(backend, options); }, deps: [MockBackend, BaseRequestOptions]}]); var backend = injector.get(MockBackend); var http = injector.get(Http); backend.connections.subscribe(c => connection = c); http.request('something.json').subscribe(res => { text = res.text(); }); connection.mockRespond(new Response({body: 'Something'})); expect(text).toBe('Something'); });
This property only exists in the mock implementation, not in real Backends.
connectionsArray : MockConnection[]
An array representation of connections
. This array will be updated with each connection that is created by this backend.
This property only exists in the mock implementation, not in real Backends.
pendingConnections : any
EventEmitter
of MockConnection
instances that haven't yet been resolved (i.e. with a readyState
less than 4). Used internally to verify that no connections are pending via the verifyNoPendingRequests
method.
This property only exists in the mock implementation, not in real Backends.
verifyNoPendingRequests()
Checks all connections, and raises an exception if any connection has not received a response.
This method only exists in the mock implementation, not in real Backends.
resolveAllConnections()
Can be used in conjunction with verifyNoPendingRequests
to resolve any not-yet-resolve connections, if it's expected that there are connections that have not yet received a response.
This method only exists in the mock implementation, not in real Backends.
createConnection(req: Request) : MockConnection
Creates a new MockConnection
. This is equivalent to calling new
MockConnection()
, except that it also will emit the new Connection
to the connections
emitter of this MockBackend
instance. This method will usually only be used by tests against the framework itself, not by end-users.
exported from @angular/http/testing/index, defined in @angular/http/testing/mock_backend.ts
Please login to continue.