微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

karma 和 Jasmine:如何在构造函数中使用 @Inject InjectionToken 测试组件

如何解决karma 和 Jasmine:如何在构造函数中使用 @Inject InjectionToken 测试组件

我正在使用 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 举报,一经查实,本站将立刻删除。