ng 测试 = 很多 "verify that it is part of this module" 错误

ng test = lots of "verify that it is part of this module" errors

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

示例错误

 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>

垫子图标、垫子占位符等也存在其他类似错误...

我的 app.module 文件正在导入这些。当我发现有此类错误的类似 Whosebug 帖子时,答案是将它们导入 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...

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

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

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