[Module Config](https://i.stack.imgur.com/rTgsU.png)

  • 我想要在我的模块中完成覆盖,但似乎可以找到一种方法来引用/监视 bootstrap 程序中的customElements.define.

prize :

  • 如果能为useFactory的报道提供帮助,我们将不胜感激.

  • 到目前为止,这项测试通过了.

  it('initializes', () => {
    const module: AppModule = TestBed.inject(AppModule);
    expect(module).toBeTruthy();
  });

  it('ngDoBootstrap', () => {
    const module: AppModule = TestBed.inject(AppModule);
    spyOn(module, "ngDoBootstrap");
    module.ngDoBootstrap();
    expect(module.ngDoBootstrap).toHaveBeenCalled();
  });

[RESOLVED] enter image description here

推荐答案

[已解决]

describe("AppModule", () => {
  let fixture: ComponentFixture<AppComponent>;

  beforeEach(async () => {
    await TestBed.configureTestingModule({
      declarations: [
        AppComponent,
      ],
      imports: [
        HttpClientModule,
        AppModule,
        RouterTestingModule.withRoutes(APP_ROUTES),
      ],
      providers: [
        DynamicComponentService,
        RouterHelperService,
        Router,
        Injector,
      ],
      schemas: [CUSTOM_ELEMENTS_SCHEMA],
    }).compileComponents();

    fixture = TestBed.createComponent(AppComponent);

  });


  it("ngDoBootstrap", () => {
    const module: AppModule = TestBed.inject(AppModule);

    spyOn(module, "ngDoBootstrap").and.callFake(() => {
      const appModule = createCustomElement(AppComponent, {
        injector: TestBed.inject(Injector)
      })

      customElements.define("custom-element-name", appModule);
    }).and.callThrough();
    spyOn(customElements, "define");

    module.ngDoBootstrap();
    expect(module).toBeTruthy();
    expect(module.ngDoBootstrap).toHaveBeenCalled();
    expect(customElements.define).toHaveBeenCalled();

  })
})

Angular相关问答推荐

Angular 单元测试组件s @ Input.'变化值检测

有没有一种方法用timeout()操作符创建计数器,用于超时一个观察对象?

如何在HTMLTITLE属性中使用Angular 显示特殊字符?

Toastr在独立组件上的Angular17实现

如何包装NGB carousel ?

成Angular 的嵌套router-outlet

带独立零部件的TranslateLoader[Angular 16]

如果Rxjs间隔请求仍在进行,则不应重置,但如果用户更改输入,则应重置

Angular 单位测试表

显示 1 个数据而不是所有 ngFor - Angular 11

在 angular2 中,如何获取在为 @Input 发送的对象上更改的属性的 onChanges

为什么 Angular2 (click) 事件没有在

如何为所有模块全局声明指令?

Angular 2+ 一次性绑定

如何在不刷新整页的情况下更新组件

错误:您要查找的资源已被删除、名称已更改或暂时不可用

angular 2 http withCredentials

移除 Angular 组件创建的宿主 HTML 元素 Select 器

Angular:找不到不同的支持对象[object Object]

ngClass 中的多个条件 - Angular 4