在 Angular 6 个测试中不模拟 HttpClient

NOT mocking HttpClient in Angular 6 tests

我正在创建一个通过 HTTP 加载和发送数据的服务,并且我创建了一些需要使用真实 HttpClient(未模拟)的测试。

所以,这是它的样子:

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

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

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

然后测试代码就可以了。但是当我 运行 我得到的测试:

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

那么如何在我的测试中发送请求?

PS。现在,如果您认为我根本不应该在测试中这样做,那您就错了。这些测试和服务实际上做了一些涉及第三方 REST 的工作 API.

HttpClient 是一个服务,不能放在 imports 数组中。您应该将其替换为提供 HttpClient.

HttpClientModule