который тянет данные с одного сервиса по http. Необходимо заmock'ать response с сервиса. Как это можно сделать?
class MockService { foo(): Observable<SOMETHING> { return Observable.of(SOMETHING); } }
https://marketplace.visualstudio.com/items?itemName=SimonTest.simontest import { ComponentFixture, TestBed } from '@angular/core/testing'; import { NO_ERRORS_SCHEMA } from '@angular/core'; import { Http } from '@angular/http'; import { AppComponent } from './app.component'; describe('AppComponent', () => { let comp: AppComponent; let fixture: ComponentFixture<AppComponent>; beforeEach(() => { const httpStub = { get: () => ({ subscribe: () => ({}) }) }; TestBed.configureTestingModule({ declarations: [ AppComponent ], schemas: [ NO_ERRORS_SCHEMA ], providers: [ { provide: Http, useValue: httpStub } ] }); fixture = TestBed.createComponent(AppComponent); comp = fixture.componentInstance; }); it('can load instance', () => { expect(comp).toBeTruthy(); }); it('title defaults to: app', () => { expect(comp.title).toEqual('app'); }); describe('doRequest', () => { it('makes expected calls', () => { const httpStub = fixture.debugElement.injector.get(Http); spyOn(httpStub, 'get'); comp.doRequest(); expect(httpStub.get).toHaveBeenCalled(); }); }); });
Обсуждают сегодня