简体   繁体   English

覆盖Angular TestBed中另一个模块中的组件

[英]Override component in another module in Angular TestBed

I'm writing TestBed unit tests. 我正在编写TestBed单元测试。

There is a certain component, which is a child of the component-under-test. 有一个组件,它是被测组件的子组件。 That child component causes errors while the test is running. 该子组件在测试运行时会导致错误。 That child component is not relevant to the test itself; 该子组件与测试本身无关; it's just causing problems. 这只是造成问题。

I would like to replace it with a dummy, or prevent it from being added. 我想用假人替换它,或者防止它被添加。

The problematic component is from a module other than the component-under-test's module. 有问题的组分是来自不同于组分被测的模块以外的模块。

I tried to make a stub/dummy: 我试图制作一个存根/假人:

@Component({
  selector : 'problematic-component-selector',
  template  : 'FAKE CAPTCHA',
})
export class ProblematicComponentStubComponent {

}

Here is my beforeEach : 这是我的beforeEach

beforeEach(async(() => {
  TestBed.configureTestingModule({
    imports: [
      ReactiveFormsModule,
      FormsModule,
      RouterModule,
      ModuleOfProblematicComponent,
    ],
    declarations: [
      ComponentUnderTest,
      ProblematicComponentStubComponent, /* NOTE: here I tried to declare the fake one */
    ],
    providers: [
      { provide: Router, useClass: RouterStub },
      { provide: ActivatedRoute, useClass: Stub },
    ],

I tried to override the components template, to prevent the errors: 我试图覆盖组件模板,以防止错误:

TestBed.overrideComponent(ProblematicComponent, {
  set: {
    template: 'Fake Captcha' // prevent ReCaptcha error
  }
})

I know about NO_ERRORS_SCHEMA , but it did not help. 我知道NO_ERRORS_SCHEMA ,但它没有帮助。

I was also experimenting with overrideModule , without success: 我也在尝试使用overrideModule ,但没有成功:

TestBed.overrideModule(ModuleOfProblematicComponent, {
  remove: {
    declarations: [ProblematicComponent],
  },
  add: {
    declarations: [ProblematicComponentStubComponent],
  }
});

So, question is: is it possible to override the ProblematicComponent (which is in a module other than the module of the ComponentUnderTest ? 所以,问题是:是否有可能覆盖ProblematicComponent (它位于ComponentUnderTest模块以外的模块中?

The TestBed#overrideComponent method will help you to replace template of any component within TestBed. TestBed#overrideComponent方法将帮助您替换TestBed中任何组件的模板。 If it's not enough, use TestBed#overrideModule to replace the whole component (template and class). 如果还不够,请使用TestBed#overrideModule替换整个组件(模板和类)。

Docs are loose: https://angular.io/api/core/testing/TestBed 文档是松散的: https//angular.io/api/core/testing/TestBed

But examples are more helpful: https://github.com/angular/angular/blob/master/aio/content/examples/testing/src/app/app.component.spec.ts#L74 但是示例更有用: https//github.com/angular/angular/blob/master/aio/content/examples/testing/src/app/app.component.spec.ts#L74

Tests may help too: https://github.com/angular/angular/blob/master/packages/platform-browser/test/testing_public_spec.ts 测试也可能有所帮助: https//github.com/angular/angular/blob/master/packages/platform-b​​rowser/test/testing_public_spec.ts

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

相关问题 Angular2 / 4:覆盖另一个模块的组件 - Angular2/4 : Override component from another module 为什么在角“ TestBed”中找不到模块? - why module in not found in angular “TestBed”? Angular 2 单元测试:如何覆盖 TestBed 中的单个提供程序(用于服务的单元测试,而不是具有服务依赖项的组件)? - Angular 2 unit tests: How to override a single provider in TestBed (for unit tests for a service, not a component, with service dependencies)? 在 TestBed 中创建角度组件时,如何覆盖 jasmine 中 ngOnInit 中的函数? - How to override a function inside ngOnInit in jasmine when creating an angular component in TestBed? Angular 中没有 TestBed 和组件的单元测试指令 - Unit Tesing directives in Angular without TestBed & Component Testbed的angular2测试组件出现错误 - error with angular2 testing component with Testbed Angular Testbed测试路线找不到模块 - Angular testbed testing route cannot find module 导航到另一个模块中的组件(Angular) - Navigate to component in another module (Angular) Angular 2 - 如何覆盖另一个模块中的依赖项进行测试 - Angular 2 - How to Override a Dependency in Another Module for Testing TestBed:通过模块导入时,组件无法编译 - TestBed: Component doesn't compile when imported through a module
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM