Skip to content

Instantly share code, notes, and snippets.

@izifortune
Last active October 10, 2022 13:45
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save izifortune/192cf86f7b28b48c56be3d57462185eb to your computer and use it in GitHub Desktop.
Save izifortune/192cf86f7b28b48c56be3d57462185eb to your computer and use it in GitHub Desktop.
import { TestBed, inject } from '@angular/core/testing';
import {
HttpClientTestingModule,
HttpTestingController
} from '@angular/common/http/testing';
import {
HttpClient,
} from '@angular/common/http';
import { SampleService } from './sample.service';
const mockAirports = {
DUB: { name: 'Dublin' },
WRO: { name: 'Wroclaw' },
MAD: { name: 'Madrid' }
};
describe('Service: SampleService', () => {
let httpMock: HttpTestingController;
let service: SampleService;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [ HttpClientTestingModule ],
providers: [
SampleService
]
});
});
beforeEach(
inject([SampleService, HttpTestingController], (_service, _httpMock) => {
service = _service;
httpMock = _httpMock;
}));
it('fetchAll$: should return a sorted list', () => {
service.fetchAll$().subscribe(airports => {
expect(airports.length).toBe(3);
expect(airports[2][0]).toBe('WRO');
});
const req = httpMock.expectOne('https://foo.bar.com/airports');
req.flush(mockAirports);
httpMock.verify();
});
it('fetchByIATA$: should the selected airport', () => {
service.fetchByIATA$('MAD').subscribe(airport => {
expect(airport.name).toBe('Madrid');
});
const req = httpMock.expectOne('https://foo.bar.com/airports');
req.flush(mockAirports);
httpMock.verify();
});
});
@danishsalam
Copy link

Great! How to create a testbed for a component with multiple service dependencies?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment