# Unit-Test et HttpClient

Pour "mocker" les appels HTTP, pas besoin d'utiliser les "spies" sur les méthodes de la classe `HttpClient`.

**Le module `HttpClientTestingModule` fournit les services nécessaires pour contrôler les échanges HTTP.**

```typescript
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);
    
    }));
    
});
```

{% hint style="success" %}
Pensez à toujours ajouter l'`afterEach` ci-dessous afin de vérifier qu'il n'y a aucune requête en attente !&#x20;

```typescript
afterEach(() => {
    httpTestingController.verify();
});
```

{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://guide-angular.wishtack.io/angular/testing/unit-testing/unit-test-et-httpclient.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
