Angular 6 - NullInjectorError: No provider for HttpClient in unit tests

Discussão em 'Angular' iniciado por Kingamere, Dezembro 5, 2024.

  1. Kingamere

    Kingamere Guest

    I am importing and using HttpClient in a service as follows:

    import { Injectable } from '@angular/core';
    import { HttpClient } from '@angular/common/http';

    @Injectable({
    providedIn: 'root',
    })
    export class MyService {
    constructor(private http: HttpClient) { }

    getData() {
    return this.http.get("url...");
    }
    }


    However, when I run ng test for my unit tests, and when those tests use the service, I am getting the error:

    Error: StaticInjectorError(DynamicTestModule)[HttpClient]:
    StaticInjectorError(Platform: core)[HttpClient]:
    NullInjectorError: No provider for HttpClient!


    The Angular 6 documentation on HTTP just says to do what I did above.

    Continue reading...

Compartilhe esta Página