You can use the HttpTestingController, available through the core of TestBed, as for me it feels more intuitive (each of them, of course). Unconfirmed fragment:
import { TestBed, async } from '@angular/core/testing'; import { HttpTestingController } from '@angular/common/http/testing'; import { MyApiService } from './my-api.service'; export function main() { describe('Test set', () => { let httpMock: HttpTestingController; beforeEach(() => { TestBed.configureTestingModule({ imports: [], providers: [MyApiService] }); httpMock = TestBed.get(HttpTestingController); }); it('should call get', async(() => { const data: any = {mydata: 'test'}; let actualResponse: any = null; MyApiService.get().subscribe((response: any) => { actualResponse = response; }); httpMock.expectOne('localhost:5555/my-path').flush(data); expect(actualResponse).toEqual(data); })); }); }
source share