docs(Http): fix and extend samples for testing/MockBackend (#13689)
Fix samples for MockBackend and MockBackend.connections that were outdated. Also extend central sample for MockBackend to ease getting started.
This commit is contained in:
parent
6b02b80a03
commit
1e6440e81b
|
@ -114,25 +114,78 @@ export class MockConnection implements Connection {
|
||||||
* ### Example
|
* ### Example
|
||||||
*
|
*
|
||||||
* ```
|
* ```
|
||||||
* import {BaseRequestOptions, Http} from '@angular/http';
|
* import {Injectable, ReflectiveInjector} from '@angular/core';
|
||||||
* import {MockBackend} from '@angular/http/testing';
|
* import {async, fakeAsync, tick} from '@angular/core/testing';
|
||||||
* it('should get some data', inject([AsyncTestCompleter], (async) => {
|
* import {BaseRequestOptions, ConnectionBackend, Http, RequestOptions} from '@angular/http';
|
||||||
* var connection;
|
* import {Response, ResponseOptions} from '@angular/http';
|
||||||
* var injector = Injector.resolveAndCreate([
|
* import {MockBackend, MockConnection} from '@angular/http/testing';
|
||||||
* MockBackend,
|
*
|
||||||
* {provide: Http, useFactory: (backend, options) => {
|
* const HERO_ONE = 'HeroNrOne';
|
||||||
* return new Http(backend, options);
|
* const HERO_TWO = 'WillBeAlwaysTheSecond';
|
||||||
* }, deps: [MockBackend, BaseRequestOptions]}]);
|
*
|
||||||
* var http = injector.get(Http);
|
* @Injectable()
|
||||||
* var backend = injector.get(MockBackend);
|
* class HeroService {
|
||||||
* //Assign any newly-created connection to local variable
|
* constructor(private http: Http) {}
|
||||||
* backend.connections.subscribe(c => connection = c);
|
*
|
||||||
* http.request('data.json').subscribe((res) => {
|
* getHeroes(): Promise<String[]> {
|
||||||
* expect(res.text()).toBe('awesome');
|
* return this.http.get('myservices.de/api/heroes')
|
||||||
* async.done();
|
* .toPromise()
|
||||||
|
* .then(response => response.json().data)
|
||||||
|
* .catch(e => this.handleError(e));
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* private handleError(error: any): Promise<any> {
|
||||||
|
* console.error('An error occurred', error);
|
||||||
|
* return Promise.reject(error.message || error);
|
||||||
|
* }
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* describe('MockBackend HeroService Example', () => {
|
||||||
|
* beforeEach(() => {
|
||||||
|
* this.injector = ReflectiveInjector.resolveAndCreate([
|
||||||
|
* {provide: ConnectionBackend, useClass: MockBackend},
|
||||||
|
* {provide: RequestOptions, useClass: BaseRequestOptions},
|
||||||
|
* Http,
|
||||||
|
* HeroService,
|
||||||
|
* ]);
|
||||||
|
* this.heroService = this.injector.get(HeroService);
|
||||||
|
* this.backend = this.injector.get(ConnectionBackend) as MockBackend;
|
||||||
|
* this.backend.connections.subscribe((connection: any) => this.lastConnection = connection);
|
||||||
* });
|
* });
|
||||||
* connection.mockRespond(new Response('awesome'));
|
*
|
||||||
* }));
|
* it('getHeroes() should query current service url', () => {
|
||||||
|
* this.heroService.getHeroes();
|
||||||
|
* expect(this.lastConnection).toBeDefined('no http service connection at all?');
|
||||||
|
* expect(this.lastConnection.request.url).toMatch(/api\/heroes$/, 'url invalid');
|
||||||
|
* });
|
||||||
|
*
|
||||||
|
* it('getHeroes() should return some heroes', fakeAsync(() => {
|
||||||
|
* let result: String[];
|
||||||
|
* this.heroService.getHeroes().then((heroes: String[]) => result = heroes);
|
||||||
|
* this.lastConnection.mockRespond(new Response(new ResponseOptions({
|
||||||
|
* body: JSON.stringify({data: [HERO_ONE, HERO_TWO]}),
|
||||||
|
* })));
|
||||||
|
* tick();
|
||||||
|
* expect(result.length).toEqual(2, 'should contain given amount of heroes');
|
||||||
|
* expect(result[0]).toEqual(HERO_ONE, ' HERO_ONE should be the first hero');
|
||||||
|
* expect(result[1]).toEqual(HERO_TWO, ' HERO_TWO should be the second hero');
|
||||||
|
* }));
|
||||||
|
*
|
||||||
|
* it('getHeroes() while server is down', fakeAsync(() => {
|
||||||
|
* let result: String[];
|
||||||
|
* let catchedError: any;
|
||||||
|
* this.heroService.getHeroes()
|
||||||
|
* .then((heroes: String[]) => result = heroes)
|
||||||
|
* .catch((error: any) => catchedError = error);
|
||||||
|
* this.lastConnection.mockRespond(new Response(new ResponseOptions({
|
||||||
|
* status: 404,
|
||||||
|
* statusText: 'URL not Found',
|
||||||
|
* })));
|
||||||
|
* tick();
|
||||||
|
* expect(result).toBeUndefined();
|
||||||
|
* expect(catchedError).toBeDefined();
|
||||||
|
* }));
|
||||||
|
* });
|
||||||
* ```
|
* ```
|
||||||
*
|
*
|
||||||
* This method only exists in the mock implementation, not in real Backends.
|
* This method only exists in the mock implementation, not in real Backends.
|
||||||
|
@ -149,27 +202,30 @@ export class MockBackend implements ConnectionBackend {
|
||||||
* ### Example
|
* ### Example
|
||||||
*
|
*
|
||||||
* ```
|
* ```
|
||||||
* import {Http, BaseRequestOptions, Response} from '@angular/http';
|
* import {ReflectiveInjector} from '@angular/core';
|
||||||
* import {MockBackend} from '@angular/http/testing';
|
* import {fakeAsync, tick} from '@angular/core/testing';
|
||||||
* import {Injector, provide} from '@angular/core';
|
* import {BaseRequestOptions, ConnectionBackend, Http, RequestOptions} from '@angular/http';
|
||||||
|
* import {Response, ResponseOptions} from '@angular/http';
|
||||||
|
* import {MockBackend, MockConnection} from '@angular/http/testing';
|
||||||
*
|
*
|
||||||
* it('should get a response', () => {
|
* it('should get a response', fakeAsync(() => {
|
||||||
* var connection; //this will be set when a new connection is emitted from the backend.
|
* let connection:
|
||||||
* var text; //this will be set from mock response
|
* MockConnection; // this will be set when a new connection is emitted from the
|
||||||
* var injector = Injector.resolveAndCreate([
|
* // backend.
|
||||||
* MockBackend,
|
* let text: string; // this will be set from mock response
|
||||||
* {provide: Http, useFactory: (backend, options) => {
|
* let injector = ReflectiveInjector.resolveAndCreate([
|
||||||
* return new Http(backend, options);
|
* {provide: ConnectionBackend, useClass: MockBackend},
|
||||||
* }, deps: [MockBackend, BaseRequestOptions]}]);
|
* {provide: RequestOptions, useClass: BaseRequestOptions},
|
||||||
* var backend = injector.get(MockBackend);
|
* Http,
|
||||||
* var http = injector.get(Http);
|
* ]);
|
||||||
* backend.connections.subscribe(c => connection = c);
|
* let backend = injector.get(ConnectionBackend);
|
||||||
* http.request('something.json').subscribe(res => {
|
* let http = injector.get(Http);
|
||||||
* text = res.text();
|
* backend.connections.subscribe((c: MockConnection) => connection = c);
|
||||||
* });
|
* http.request('something.json').toPromise().then((res: any) => text = res.text());
|
||||||
* connection.mockRespond(new Response({body: 'Something'}));
|
* connection.mockRespond(new Response(new ResponseOptions({body: 'Something'})));
|
||||||
* expect(text).toBe('Something');
|
* tick();
|
||||||
* });
|
* expect(text).toBe('Something');
|
||||||
|
* }));
|
||||||
* ```
|
* ```
|
||||||
*
|
*
|
||||||
* This property only exists in the mock implementation, not in real Backends.
|
* This property only exists in the mock implementation, not in real Backends.
|
||||||
|
|
Loading…
Reference in New Issue