NullInjectorError:HttpClient没有提供程序(已导入)

时间:2019-06-04 09:49:43

标签: angular unit-testing karma-jasmine angular-httpclient

我正在尝试使用httpclient函数测试我的服务,但是当我运行业力时,总会得到标题中提到的错误。

我已经在我的规范文件中导入了HttpClientModule

编辑:
因此,我发现我的测试正在正常进行,只是控制台中印有此错误消息,这令人困惑。我认为那不是应该的样子……

app.module.ts

import {BrowserModule} from '@angular/platform-browser';
import {NgModule} from '@angular/core';

import {AppRoutingModule} from './app-routing.module';
import {AppComponent} from './app.component';
import {HttpClientModule} from '@angular/common/http';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
import {MaterialModules} from './material-module';
import {DockerService} from './Services/docker.service';

@NgModule({
    declarations: [
        AppComponent
    ],
    imports: [
        BrowserModule,
        AppRoutingModule,
        HttpClientModule,
        BrowserAnimationsModule,
        MaterialModules
    ],
    providers: [DockerService],
    bootstrap: [AppComponent],
    entryComponents: []
})
export class AppModule {
}

docker-service.ts

import {Injectable} from '@angular/core';
import {HttpClient, HttpHeaders} from '@angular/common/http';
import {Observable} from 'rxjs';

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

docker-service.spec.ts

import {TestBed} from '@angular/core/testing';
import {DockerService} from './docker.service';
import {HttpClientTestingModule} from '@angular/common/http/testing';
import {HttpClientModule} from '@angular/common/http';

describe('DockerService', () => {
  beforeEach(() => {
          TestBed.configureTestingModule({
              imports: [HttpClientTestingModule],
              providers: [DockerService]
          });
    });
});
'Unhandled Promise rejection:', 'StaticInjectorError(DynamicTestModule)[DockerService -> HttpClient]: 
  StaticInjectorError(Platform: core)[DockerService -> HttpClient]: 
    NullInjectorError: No provider for HttpClient!', '; Zone:', 'ProxyZone', '; Task:', 'Promise.then', '; Value:', NullInjectorError: StaticInjectorError(DynamicTestModule)[DockerService -> HttpClient]: 
  StaticInjectorError(Platform: core)[DockerService -> HttpClient]: 
    NullInjectorError: No provider for HttpClient!

1 个答案:

答案 0 :(得分:-1)

尝试从“ @ angular / common / http / testing ”导入HttpClientTestingModule,HttpTestingController。

请参阅下面我测试角度服务的方式。

import { TestBed, inject } from '@angular/core/testing';
import { WorkloadsService } from './workloads.service';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';

describe('WorkloadsService', () => {
  beforeEach(() => {
    TestBed.configureTestingModule({
    providers: [WorkloadsService],
    imports: [HttpClientTestingModule]
  });
});

it('should call workload services',inject([HttpTestingController, WorkloadsService],
(httpMock: HttpTestingController, backend: WorkloadsService) => {

  backend.getWorkload().subscribe((event) => {
    expect(event['validation']).toBe("success");
  });

  let mockUrl = 'http://localhost/project/workloads/';

  const mockRequestRun = httpMock.expectOne(mockUrl);
    expect(mockRequestRun.request.method).toBe('GET');
}));
})