StaticInjectorError(DynamicTestModule)-NullInjectorError:注射令牌fuseCustomConfig没有提供程序

时间:2019-08-16 09:15:27

标签: angular typescript unit-testing karma-jasmine

我刚刚开始测试,所以需要一些帮助。

我有一个角度LoginModule,其中包含要测试的LoginComponent

这是LoginModule配置

@NgModule({
    declarations: [
        LoginComponent
    ],
    imports: [
        CommonModule,
        ReactiveFormsModule,
        RouterModule.forChild(routes),

        MatFormFieldModule,
        MatInputModule,
        MatCheckboxModule,
        MatSnackBarModule,
        MatButtonModule,
        MatRadioModule
    ]
})
export class LoginModule { }

LoginComponent依赖于类似的服务

constructor(
        private fuseConfig: FuseConfigService,
        private formBuilder: FormBuilder,
        private router: Router,
        private authenticationService: AuthService,
        private route: ActivatedRoute,
        private snackBar: MatSnackBar,
        private fuseNavigationService: FuseNavigationService,
        private pbiService: PbiService,
        private translateService: TranslateService
    ) {
        this.incorrectCredentials = false;
        this.loginFormErrors = {
            email: {},
            password: {},
        };
      }

这就是我编写规范文件的方式

import { TestBed, fakeAsync, ComponentFixture } from "@angular/core/testing";
import { DebugElement } from "@angular/core";
import { By } from "@angular/platform-browser";

import { LoginComponent } from "./login.component";
import { LoginModule } from "./login.module";
import { Router } from "@angular/router";
import { FuseConfigService } from "@fuse/services/config.service";
import { FuseNavigationService } from "@fuse/components/navigation/navigation.service";

describe("Login Module -> ", () => {

    let component: LoginComponent;
    let fixture: ComponentFixture<LoginComponent>;

    beforeEach(() => {
        // refine the test module by declaring the test component
        TestBed.configureTestingModule({
            imports: [LoginModule],
            providers: [
                { provide: Router, useClass: class { navigate = jasmine.createSpy("navigate"); } },
                FuseConfigService, FuseNavigationService
            ]
        });

        // create component and test fixture
        fixture = TestBed.createComponent(LoginComponent);

        // get test component from the fixture
        component = fixture.componentInstance;
    });

    it("Login page has title and login button", fakeAsync(() => {
        const elTitle: DebugElement = fixture.debugElement.query(By.css(".la-title"));
        expect(elTitle.nativeElement.textContent.trim()).toBe("");
        fixture.detectChanges();
        expect(elTitle.nativeElement.textContent.trim()).toBe("Login");
    }));
});

但是我遇到了以下错误

Error: StaticInjectorError(DynamicTestModule)[FuseConfigService -> InjectionToken fuseCustomConfig]: 
  StaticInjectorError(Platform: core)[FuseConfigService -> InjectionToken fuseCustomConfig]: 
    NullInjectorError: No provider for InjectionToken fuseCustomConfig!

我想念什么吗?

这是从模块测试组件的正确方法,是指导入模块本身,还是仅在TestBed.configureTestingModule中声明组件?

0 个答案:

没有答案