Angular karma和Jasmine:如何在构造函数中使用@InjectInjectionToken测试组件
我正在使用angular 10,在我的组件的构造函数中声明一些注入变量,并在单元测试文件中配置注入值,当运行ng test时,它给出以下错误:Angular karma和Jasmine:如何在构造函数中使用@InjectInjectionToken测试组件,angular,unit-testing,karma-jasmine,angular-dependency-injection,Angular,Unit Testing,Karma Jasmine,Angular Dependency Injection,我正在使用angular 10,在我的组件的构造函数中声明一些注入变量,并在单元测试文件中配置注入值,当运行ng test时,它给出以下错误: Error: Token InjectionToken GoldenLayoutComponentHost is missing a ɵprov definition. my componentexample.ts中的构造函数是: constructor( @Inject(GoldenLayoutCo
Error: Token InjectionToken GoldenLayoutComponentHost is missing a ɵprov definition.
my 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>;
从'@angular/core'导入{InjectionToken};
导出声明常量GoldenLayoutContainer:InjectionToken;
导出声明常量GoldenLayoutComponentState:InjectionToken;
出口申报常数GoldenLayoutEventHub:InjectionToken;
导出声明常量GoldenLayoutComponentHost:InjectionToken;
我的单元测试组件example.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();
});
description('测试组件',()=>{
let组件:组件示例;
let夹具:组件夹具;
const mockGoldenLayoutComponentHost={};
const mockGoldenLayoutContainer={};
常量componentTypes:ComponentType[]=[
];
在每个之前(()=>{
TestBed.configureTestingModule({
进口:[
HttpClientTestingModule,
MatDialogModule,
GoldenLayoutModule.forRoot(组件类型),
LoggerModule.forRoot({
级别:NgxLoggerLevel.DEBUG,
serverLogLevel:NgxLoggerLevel.ERROR
}),
过滤管模块,
ToAssetModule.forRoot(),
],
声明:[
例如组件,
确认对话框组件
],
提供者:[
{provide:GoldenLayoutComponentHost,usevalue:{mockGoldenLayoutComponentHost},
{提供:GoldenLayoutContainer,usevalue:{mockGoldenLayoutContainer}}
],
模式:[自定义元素模式,无错误模式]
}).compileComponents();
});
在每个之前(()=>{
fixture=TestBed.createComponent(ComponentExample);
组件=fixture.componentInstance;
fixture.detectChanges();
});
fit('应该创建组件',()=>{
expect(component.toBeTruthy();
});
我读过几篇关于stackoverflow的帖子,说这是在测试环境中提供injectionToken的方法,但我运气不好
我做错什么了吗?
提前谢谢