Search code examples
angularangular-httpclientangular-test

NOT mocking HttpClient in Angular 6 tests


I'm creating a service that loads and sends data over HTTP and I created some tests that need to use real HttpClient (NOT mocked).

So, here's how it looks like:

describe("My3rdPartyConnectedServiceTests", () => {
  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [HttpClient],
      providers: [My3rdPartyConnectedService]
    });
  });

  beforeEach(async(() => {
    const http = TestBed.get(HttpClient);

    http.delete("http://localhost:22213/api/somemethod").subscribe(() => {
…
    });
  }));

and the tests code goes. But when I run the tests I get:

Unexpected value 'HttpClient' imported by the module 'DynamicTestModule'. Please add a @NgModule annotation.

So how can I send requests inside my tests?

PS. Now, if you think I shouldn't be doing this in tests at all, you're wrong. These tests and the service actually do some work that involves third-part REST API.


Solution

  • HttpClient is a service and cannot be put in imports array. You should replace it with HttpClientModule which provides HttpClient.