Testing using MockBackend

A more sensible approach is to replace HTTP calls with a MockBackend. For doing this, we can use the beforeEach() method. This will allow us to mock our responses and avoid hitting the real backend, thereby boosting our tests:

//users.serviceHttp.spec.tsdescribe('MockBackend: UsersServiceHttp', () => {let mockbackend, service;//setupbeforeEach(() => {TestBed.configureTestingModule({imports: [ HttpModule ],providers: [UsersServiceHttp,{ provide: XHRBackend, useClass: MockBackend }]})});beforeEach(inject([UsersServiceHttp, XHRBackend], (_service, _mockbackend) => {service = _service;mockbackend = _mockbackend;}));//specsit('should return mocked response (sync)', () => {       let response = ["ru", "es"];      mockbackend.connections.subscribe(connection ...

Get TypeScript 2.x for Angular Developers now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.