如何在'HttpClient`和`HttpClientModule`中使用NProgress?

问题描述 投票:3回答:1

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

});
angular unit-testing
1个回答
0
投票

问题已修复...解决方案是在configureTestingModule({...})中的Providers数组中添加以下代码方法:

...
providers: [
    { provide: MatDialogRef, useClass: MatDialogRefMock },
    ...
    { provide: NgProgress }
],
....
© www.soinside.com 2019 - 2024. All rights reserved.