简体   繁体   English

自动模拟 Angular 组件

[英]Auto-Mock Angular Components

Problem问题

When testing Angular Components, I often stumble upon the following error message:在测试 Angular 组件时,我经常偶然发现以下错误消息:

'NG0304: 'app-chip' is not a known element:
1. If 'app-chip' is an Angular component, then verify that it is part of this module.
2. If 'app-chip' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.'

Reason原因

It's usually because I use a Component (here: <app-chip> ) in the template:这通常是因为我在模板中使用了一个组件(这里: <app-chip> ):

<div class="asanas-filter">
  <app-filter-sticky-header [optionIds]="['asanas']"> Asanas </app-filter-sticky-header>

  <div class="chips" *ngIf="asanas">
    <app-chip
      *ngFor="let asana of asanas"
      [label]="asana.label"
      [(model)]="model[asana.id]"
    ></app-chip>
  </div>
  <app-filter-footer [optionIds]="['asanas']" [groupOption]="true"></app-filter-footer>
</div>

and forgot to add the mocked Component to the Spec-File:并忘记将模拟组件添加到规范文件中:

TestBed.configureTestingModule({
  declarations: [
    AsanasFilterComponent,
    MockComponent(FilterStickyHeaderComponent),
    MockComponent(FilterFooterComponent),
    // MockComponent(AppChipComponent) is missing here
  ],
}).compileComponents();

I am using ng-mocks for mocking Components.我正在为 mocking 组件使用ng-mocks

Desired Solution所需的解决方案

I think, having to manually add the components used in the template to the Test Config does not benefit the development process: If I forgot to import a component I'm using into the current module (or in case of a typo), the build will fail anyway.我认为,必须手动将模板中使用的组件添加到测试配置中不会有利于开发过程:如果我忘记将我正在使用的组件导入到当前模块中(或者在拼写错误的情况下),构建无论如何都会失败。 (because of that I don't want to use CUSTOM_ELEMENTS_SCHEMA ) (因为我不想使用CUSTOM_ELEMENTS_SCHEMA

Is there a way to automatically add all the components I use in the Template to the declarations-Array as Mocks?有没有办法自动将我在模板中使用的所有组件作为 Mocks 添加到声明数组中?

Or is there a reason to not do it (and keep adding them manually)?还是有理由不这样做(并继续手动添加它们)?

what you are looking for is MockBuilder .您正在寻找的是MockBuilder

With its help you need only a component and its module, the rest will be mocked automatically by default.在它的帮助下,您只需要一个组件及其模块,rest 将默认自动模拟。

beforeEach(() => MockBuilder(AsanasFilterComponent, AsanasFilterModule));

if AsanasFilterModule imports / declares FilterStickyHeaderComponent , FilterFooterComponent and AppChipComponent , then they'll be mocked.如果AsanasFilterModule导入/声明FilterStickyHeaderComponentFilterFooterComponentAppChipComponent ,那么它们将被模拟。

The goal here is the same like avoiding CUSTOM_ELEMENTS_SCHEMA : if a developer has deleted a declaration or an import from AsanasFilterModule , then related tests would fail due to the missing mock in MockBuilder .这里的目标与避免CUSTOM_ELEMENTS_SCHEMA相同:如果开发人员从AsanasFilterModule中删除了声明或导入,则由于MockBuilder中缺少模拟,相关测试将失败。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM