Pour "mocker" les appels HTTP, pas besoin d'utiliser les "spies" sur les méthodes de la classe HttpClient
.
describe('PickWeatherStation', () => {
let httpTestingController: HttpTestingController;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [ HttpClientTestingModule ]
})
.compileComponents();
}));
let weatherStation: PickyWeatherStation;
beforeEach(() => weatherStation = TestBed.get(PickyWeatherStation));
let httpTestingController: HttpTestingController;
beforeEach(() => httpTestingController = TestBed.get(HttpTestingController));
afterEach(() => {
httpTestingController.verify();
});
it('should get temperature from API', fakeAsync(() => {
let temperature: number;
weatherStation.getTemperature('Lyon')
.subscribe(_temperature => temperature = _temperature);
const req = httpTestingController.expectOne('/city-weather/lyon');
req.flush({
temperature: 30
});
expect(temperature).toEqual(30);
}));
});