NullInjectorError: 没有 HttpTestingController 的提供者!在角度测试期间

der*_*e23 1 testing typescript angular

当我尝试测试我的服务时出现此错误我做了本教程中的所有操作

          NullInjectorError: No provider for HttpTestingController!
        error properties: Object({ ngTempTokenPath: null, ngTokenPath: [ 'HttpTestingController', 'HttpTestingController' ] })
            at <Jasmine>
            at NullInjector.get (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:1076:1)
            at R3Injector.get (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:16629:1)
            at R3Injector.get (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:16629:1)
            at NgModuleRef$1.get (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:36024:1)
            at TestBedRender3.inject (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/testing.js:3111:53)
            at Function.get (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/testing.js:3005:1)
            at UserContext.beforeEach (http://localhost:9876/_karma_webpack_/src/app/services/weatherService/weather.service.spec.ts:18:24)
            at ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:364:1)
            at ProxyZoneSpec.push../node_modules/zone.js/dist/zone-testing.js.ProxyZoneSpec.onInvoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist
Run Code Online (Sandbox Code Playgroud)

天气数据

export class WeatherDto {
    name: String;
    weather: String;
    weatherIcon: String;
    temp: Number;
    pressure: Number;
    minTemp: Number;
    maxTemp: Number;
}
Run Code Online (Sandbox Code Playgroud)

气象服务

import { Injectable } from '@angular/core';
import { environment } from 'src/environments/environment';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs';
import { WeatherDto } from 'src/app/models/weather-dto.model';


@Injectable({
  providedIn: 'root'
})
export class WeatherService {
  requestUrl = environment.baseUrl + environment.weatherUrl;
  token = environment.token;

  constructor(private http: HttpClient) { }

  getWeather(): Observable<WeatherDto> {
    var header = {
      headers: new HttpHeaders()
        .set('Authorization', `Bearer ${this.token}`)
        .set('Content-Type', 'application/json')

    }
    return this.http.get<WeatherDto>(this.requestUrl, header);
  }
}

Run Code Online (Sandbox Code Playgroud)

测试

import { TestBed } from '@angular/core/testing';
import { HttpClientModule } from '@angular/common/http';
import { HttpTestingController } from '@angular/common/http/testing';

import { WeatherService } from './weather.service';
import { WeatherDto } from 'src/app/models/weather-dto.model';

describe('WeatherService', () => {
  let service: WeatherService;
  let httpMock: HttpTestingController;

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [HttpClientModule],
      providers: [WeatherService]
    }).compileComponents();
    service = TestBed.inject(WeatherService);
    httpMock = TestBed.get(HttpTestingController);
  });

  it('be able to retrieve weather from the API via GET', () => {
    const dummyWeather: WeatherDto = {
      name: 'City',
      weather: 'Clear',
      weatherIcon: '02d',
      temp: 20.0,
      pressure: 1024.0,
      minTemp: 18.0,
      maxTemp: 22.0
    };
    service.getWeather().subscribe(data => {
      expect(data.name).toEqual('City');

    });

    const request = httpMock.expectOne(`http://localhost:8080/api/weather`);
    expect(request.request.method).toBe('GET');
    request.flush(dummyWeather);

    afterEach(() => {
      httpMock.verify();
    });
  });
})

Run Code Online (Sandbox Code Playgroud)

所以我确实点击了我的 spring boot api,我得到了很好的回应。但是当我尝试测试这项服务时,我不知道该怎么做。对我来说很奇怪我也得到了Chrome 71.0.3578 (Linux 0.0.0) WeatherComponent should create FAILED但我删除了负责这部分测试的部分。我不知道我做错了什么?!

小智 5

你应该像这样:

  HttpClientTestingModule,
  HttpTestingController,
} from '@angular/common/http/testing';


beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [HttpClientTestingModule],
      providers: [WeatherService]
    }).compileComponents();
    service = TestBed.inject(WeatherService);
    httpMock = TestBed.get(HttpTestingController);
  });
Run Code Online (Sandbox Code Playgroud)