首页 > 解决方案 > ng test = 很多“验证它是这个模块的一部分”错误

问题描述

我正在尝试编写 Jasmine 单元测试,但是当我运行 ng test 时,终端因错误而疯狂

示例错误

 1. If 'mat-radio-group' is an Angular component, then verify that it is part of this module.
        2. If 'mat-radio-group' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. ("has-error">
                      <span id="over21Label">Are you over the age of 21?</span>
                      [ERROR ->]<mat-radio-group aria-label="Select an option" name="over21" formControlName="over21">
                     "): ng:///DynamicTestModule/BuyerSignupComponent.html@118:14
        'mat-card' is not a known element:
 1. If 'mat-card' is an Angular component, then verify that it is part of this module.
        2. If 'mat-card' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. ("
              <div class="row">
                <div class="col-md-6 col-sm-8 col-lg-3 mx-auto">
                  [ERROR ->]<mat-card id="signupBox">
"): ng:///DynamicTestModule/BuyerSignupComponent.html@14:10unt Registration</div>

对于 mat-icon、mat-placeholder 等还有其他类似的错误...

我的 app.module 文件正在导入这些。当我发现类似的带有此类错误的 stackoverflow 帖子时,答案是在 app.module 中导入它们,但它们已经是。我是这样导入的

import {
  MatInputModule,
  MatCardModule,
  MatButtonModule,
  MatToolbarModule,
  MatExpansionModule,
  MatRadioModule,
  MatStepperModule,
  MatCheckboxModule,
  MatSidenavModule,
  MatSnackBarModule,
  MatIconModule,
  MatDialogModule,
  MatListModule,
  MatDividerModule,
  MatSelectModule,
  MatProgressSpinnerModule,
  MatAutocompleteModule,
  MatTableModule,
  MatRippleModule,
  MatTooltipModule
} from "@angular/material";

然后在进口

  ],
  imports: [
MatRadioModule, MatCardModule, etc...

所以它正在被导入,所以我做错了什么?

标签: angularjasminekarma-jasmine

解决方案


您需要将其作为导入添加到您的测试模块 (mycomponent.component.spec.ts)

 beforeEach(async(() => {
   TestBed.configureTestingModule({
   imports: [
     MaterialModule
   ],
   declarations: [MyCmponent],
  }).compileComponents();
}));

推荐阅读