> For the complete documentation index, see [llms.txt](https://guide-angular.wishtack.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://guide-angular.wishtack.io/angular/testing/unit-testing/testbed.md).

# TestBed

La classe **`TestBed`** est une classe Angular permettant principalement de **créer un environnement de test émulant le fonctionnement d'un module Angular**.

La méthode statique `configureTestingModule` prend en paramètre **une configuration partiellement similaire à `@NgModule()`** qui permet de déclarer les composants à tester ou les `providers` des services à tester ou **encore mieux importer le module contenant le code à tester**.

```typescript
    beforeEach(async(() => {
        TestBed.configureTestingModule({
            imports: [
                BookModule
            ]
        }).compileComponents();
    }));
```

{% hint style="success" %}
Utilisez `imports` pour **importer le module contenant le composant ou service (ou autre) à tester** afin d'éviter de redéfinir les imports des dépendances nécessaires.

Cela permet aussi de s'assurer que le module à tester (`BookModule`) est autonome et qu'il est importe bien ses propres dépendances.
{% endhint %}

{% hint style="info" %}
La méthode `compileComponents` est asynchrone *(car elle télécharge les templateUrl dans des environnement hors CLI)* et retourne une `Promise`. C'est pour cette raison que le `beforeEach` de configuration utilise la fonction [`async`](/angular/testing/unit-testing/unit-test-asynchrone.md).
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://guide-angular.wishtack.io/angular/testing/unit-testing/testbed.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
