我正在使用 Angular 10,在组件的构造函数中声明一些注入变量,并在单元测试文件中配置注入值,运行 ng test 时会出现以下错误:
Error: Token InjectionToken GoldenLayoutComponentHost is missing a ɵprov definition.
我的 componentexample.ts 中的构造函数是:
constructor(
@Inject(GoldenLayoutComponentHost) protected goldenLayout: GoldenLayoutComponent,
@Inject(GoldenLayoutContainer) protected container: GoldenLayout.Container) {
super(logger, dialog, toastr, authorizationService, goldenLayout, container)}
我对 token.d.ts 的配置是:
import { InjectionToken } from '@angular/core';
export declare const GoldenLayoutContainer: InjectionToken<unknown>;
export declare const GoldenLayoutComponentState: InjectionToken<unknown>;
export declare const GoldenLayoutEventHub: InjectionToken<unknown>;
export declare const GoldenLayoutComponentHost: InjectionToken<unknown>;
我的单元测试 componentexample.spec.ts 是:
describe('Test a component ', () => {
let component: ComponentExample;
let fixture: ComponentFixture<ComponentExample>;
const mockGoldenLayoutComponentHost = {};
const mockGoldenLayoutContainer = {};
const componentTypes: ComponentType[] = [
];
beforeEach( () => {
TestBed.configureTestingModule({
imports: [
HttpClientTestingModule,
MatDialogModule,
GoldenLayoutModule.forRoot(componentTypes),
LoggerModule.forRoot({
level: NgxLoggerLevel.DEBUG,
serverLogLevel: NgxLoggerLevel.ERROR
}),
FilterPipeModule,
ToastrModule.forRoot(),
],
declarations: [
ExampleComponent,
ConfirmationDialogComponent
],
providers: [
{provide: GoldenLayoutComponentHost, usevalue: {mockGoldenLayoutComponentHost} },
{provide: GoldenLayoutContainer, usevalue: {mockGoldenLayoutContainer} }
],
schemas: [CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA]
}).compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(ComponentExample);
component = fixture.componentInstance;
fixture.detectChanges();
});
fit('should create the component', () => {
expect(component).toBeTruthy();
});
我已经阅读了有关 stackoverflow 的几篇文章,这是在测试环境中提供 injectionToken 的方法。但我没有运气。
难道我做错了什么?提前致谢。