错误:失败:模板解析错误:'mat-checkbox'不是已知元素

9 testing typescript angular-material angular

我创建此代码来测试我的组件.

我试过这段代码:

describe('Component: AddAlarms', () => {
    let component: AddAlarmsFormComponent;
    let fixture: ComponentFixture<AddAlarmsFormComponent>;
    beforeEach(() => {
        TestBed.configureTestingModule({
            declarations: [AddAlarmsFormComponent]
        });
        fixture = TestBed.createComponent(AddAlarmsFormComponent);
        component = fixture.componentInstance;
    });
});
Run Code Online (Sandbox Code Playgroud)

运行时ng test显示此错误:

Failed: Template parse errors:
'mat-checkbox' is not a known element:
1. If 'mat-checkbox' is an Angular component, then verify that it is part of this module.
2. If 'mat-checkbox' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. ("
        </div>
        <div class="input-field col s2">
          [ERROR ->]<mat-checkbox class="example-margin" (click)="sortbydate()">Dates</mat-checkbox>
        </div>
     "): ng:///DynamicTestModule/NotificationsComponent.html@12:10
Run Code Online (Sandbox Code Playgroud)

我验证了我的module.ts,没关系.所以,我有这个:

import {MatCheckboxModule} from '@angular/material/checkbox';
Run Code Online (Sandbox Code Playgroud)

你能问我,问题是什么?

San*_*nde 18

您需要像imports上面declarations这样添加数组:

像这样添加:

import { MatCheckboxModule } from '@angular/material/checkbox';
Run Code Online (Sandbox Code Playgroud)

并添加如下imports数组:

TestBed.configureTestingModule({
   imports: [
        MatCheckboxModule
   ],
   declarations: [AddAlarmsFormComponent]
})
Run Code Online (Sandbox Code Playgroud)

  • 棱角分明的示例代码甚至都没有,男人的文档确实很出色。 (2认同)

Chi*_*ien 0

伟大的!

但我认为你需要导入MatCheckboxModulea或者你忘记spec.ts导入模块!"imports"ngModule

你能尝试告诉我是否有效吗?