我在运行ng测试时遇到此错误我的应用程序模块似乎正常我在模块中有所有导入,在服务测试中我有提供商[服务]我不知道为什么所有测试都失败下面是失败测试之一请提供建议谢谢
Error: StaticInjectorError(DynamicTestModule)[OrderService -> HttpClient]:
StaticInjectorError(Platform: core)[OrderService -> HttpClient]:
NullInjectorError: No provider for HttpClient!
at _NullInjector.webpackJsonp../node_modules/@angular
Error: StaticInjectorError(DynamicTestModule)[OrderService -> HttpClient]:
StaticInjectorError(Platform: core)[OrderService -> HttpClient]:
NullInjectorError: No provider for HttpClient!
这是服务级别
import { Injectable } from '@angular/core';
import {HttpClient, HttpErrorResponse, HttpHeaders, HttpResponse} from "@angular/common/http";
@Injectable()
export class OrderService {
baseUrl : string = 'http://localhost:8088/orders';
filterUrl: string = 'http://localhost:8088/orders?start=?&end=?';
private order: Order;
private headers = new HttpHeaders({'Content-Type': 'application/json'});
constructor(private http: HttpClient) { }
getOrders(From: Date, To: Date): Observable<Order[]> {
return this.http.get(this.baseUrl + '?start=' + From + '&end=' + To)
.pipe(map(this.extractData),
tap(data => console.log(JSON.stringify(data))),
catchError(this.handleError)
);
}
createOrder(order: Order): Observable<number> {
return this.http.post(this.baseUrl, JSON.stringify(order), {headers: this.headers})
.pipe(map(this.extractData),
catchError(this.handleError));
}
I应用程序模块。ts我有
imports: [
NgbModule.forRoot(),
BrowserModule,
AppRoutingModule,
FormsModule,
HttpClientModule,
ReactiveFormsModule
],
providers: [OrderService,TruckService],
export class AppModule {
constructor(router: Router) {
console.log('Routes: ', JSON.stringify(router.config, undefined, 2));
}
}
这就是测试
beforeEach(() => {
TestBed.configureTestingModule({
imports: [ HttpClientTestingModule ],
providers: [ TruckService ]
});
});
afterEach(inject([HttpTestingController], (httpClient: HttpTestingController) => {
httpClient.verify();
}));
it(`should create`, async(inject([TruckService, HttpTestingController],
(service: TruckService, httpClient: HttpTestingController) => {
expect(service).toBeTruthy();
})));