何使用NgProgress与`HttpClient`和`HttpClientModule`?

时间:2018-03-05 18:12:25

标签: angular unit-testing

我正在使用 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();
        })
    );

});

1 个答案:

答案 0 :(得分:0)

问题已修复...解决方案是在configureTestingModule({...})方法:

中的Providers数组中添加以下代码
...
providers: [
    { provide: MatDialogRef, useClass: MatDialogRefMock },
    ...
    { provide: NgProgress }
],
....