我正在使用 NgProgress
作为组件,它运行良好,并且在运行单元测试时也是如此。
我确实使用 HttpModule
实现了 HttpClientModule
的升级,看起来不错。我的问题是,当我运行单元测试时,我得到以下错误:
[object ErrorEvent] thrown
Error: StaticInjectorError[NgProgress]:
StaticInjectorError[NgProgress]:
NullInjectorError: No provider for NgProgress!
错误通常是解释自己,但我无法摆脱它并修复它。
有没有解决方法或解决方法?我是否错过了有关 NgProgress
的任何更新或升级?
app.component.ts
:
...
import { HttpClient } from '@angular/common/http';
import { NgProgress } from '@ngx-progressbar/core';
...
constructor(..., public progress: NgProgress, ...) {...}
myFunction() {
...
this.progress.complete();
....
}
app.component.spec.ts
:
import { RouterTestingModule } from "@angular/router/testing";
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { HttpClient } from '@angular/common/http';
import { NgProgress } from '@ngx-progressbar/core';
....
describe("myComponent", () => {
let component: myComponent;
let fixture: ComponentFixture<myComponent>;
beforeEach(
async(() => {
TestBed.configureTestingModule({
imports: [
...
RouterTestingModule,
HttpClientTestingModule,
ReactiveFormsModule,
...
],
declarations: [myComponent],
providers: [
{ provide: MatDialogRef, useClass: MatDialogRefMock },
...
],
schemas: [ CUSTOM_ELEMENTS_SCHEMA ]
}).compileComponents();
})
);
});
答案 0 :(得分:0)
问题已修复...解决方案是在configureTestingModule({...})
方法:
...
providers: [
{ provide: MatDialogRef, useClass: MatDialogRefMock },
...
{ provide: NgProgress }
],
....