简体   繁体   English

如何测试动态加载组件的 Angular (v11) 组件

[英]How test an Angular (v11) component that loads components dynamically

In my code I'm following Angular v11 guide for that在我的代码中,我遵循 Angular v11 指南

@Directive({
  selector: '[stepHostContent]',
})
export class StepHostContentDirective {
  constructor(public viewContainerRef: ViewContainerRef) { }
}

@Component({
  selector: 'app-dialog-wizard',
  templateUrl: './dialog-wizard.component.html',
  styleUrls: ['./dialog-wizard.component.scss']
})
export class DialogWizardComponent implements OnInit, OnDestroy {

  @ViewChild(StepHostContentDirective, { static: true }) stepHostContent: StepHostContentDirective;

  constructor(
    public readonly dialog: MatDialogRef<DialogJobsWizardComponent>,
    @Inject(MAT_DIALOG_DATA) public readonly data: { theme: string, title: string },
    public readonly componentFactoryResolver: ComponentFactoryResolver
  ) { }
...
private loadStepContentComponent(componentToLoad: Type<any>): void {
   const componentFactory =  this.componentFactoryResolver.resolveComponentFactory(componentToLoad);
   const viewContainerRef = this.stepHostContent.viewContainerRef;
   viewContainerRef.clear();

   viewContainerRef.createComponent(componentFactory);
 }

My unit test我的单元测试

describe('DialogWizardComponent', () => {
  let component: DialogWizardComponent;
  let fixture: ComponentFixture<DialogWizardComponent>;
  let componentFactoryResolverSpy = jasmine.createSpyObj<ComponentFactoryResolver>('ComponentFactoryResolver', ['resolveComponentFactory'])
  
  beforeEach(async () => {
    componentFactoryResolverSpy = spyComponentFactoryResolverSpy();
    await TestBed.configureTestingModule({
      declarations: [ 
        DialogsWizardComponent,
        MockComponent(DialogBaseComponent),
        MockComponent(StepTestAComponent),
        MockComponent(StepTestBComponent),
        StepHostContentDirective
      ],
      providers: [
        { provide: MatDialogRef, useValue: { close: () => {} } },
        { provide: MAT_DIALOG_DATA, useValue: { theme: 'dark', title: 'Title' } },
        { provide: ComponentFactoryResolver, useValue: componentFactoryResolverSpy },
      ],
    })
    .compileComponents();
  });

  beforeEach(() => {
    fixture = TestBed.createComponent(DialogWizardComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

the error says错误说

Cannot read properties of undefined (reading 'ngModule') pointing to this line viewContainerRef.createComponent(componentFactory); Cannot read properties of undefined (reading 'ngModule') viewContainerRef.createComponent(componentFactory);

You need to mock ViewContainerRef.您需要模拟 ViewContainerRef。

https://angular.io/guide/testing-components-basics https://angular.io/guide/testing-components-basics

I fixed it removing the ComponentFactoryResolver from providers, and it worked我修复了它,从提供者那里删除了ComponentFactoryResolver ,并且它起作用了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 angular v11 + Scss 编译失败 - angular v11 + Scss failed to compile Angular V11:NullInjectorError:没有 ControlContainer 的提供程序 - Angular V11: NullInjectorError: No provider for ControlContainer Angular v8 到 v11 - 禁用常春藤的 entryComponents 问题 - Angular v8 to v11 - entryComponents issue with Ivy disabled 如何解决这个错误? angular v11 原理图工作流程失败。 看上面 - How to solve this error ? angular v11 The Schematic workflow failed. See above 在架构中找不到“tsconfig”-将 tslint 迁移到 eslint (Angular v11) - 'tsConfig' is not found in schema - Migrating tslint to eslint (Angular v11) Angular 2+ (v11) 自动将 URL 中的片段 hash (#) 编码为 %23 - Angular 2+ (v11) automatically encode fragment hash (#) in URL into %23 在 Angular v11/12 中使用 d3.stack() 实现堆积条形图时出现问题 - Problem implementing Stacked Bar Chart using d3.stack() in Angular v11/12 Angular 自定义材质主题在 v11 到 14 升级后未加载 - Angular Custom Material Theme not loading after v11 to 14 upgrade Angular 11 DI 用于动态编译组件 - Angular 11 DI for dynamically compiled components 在 Angular 11 中动态生成/生成现有组件 - spawn/generate existing components dynamically in Angular 11
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM