问题描述
我正在使用 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 的方法。但我没有运气。
我做错了吗? 提前致谢。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)