angular5组件单元测试在添加材料后被破坏

时间:2018-04-22 16:55:53

标签: angular angular-material angular5

我在组件模板中使用角度材质,例如

<mat-list role="list">
    <mat-list-item role="listitem">
        <div class='title'>
            <h3 mat-header color="primary">Loign (admin/admin or user/user)</h3>
        </div>

然后在我的组件单元测试中,

describe('LoginComponent', ()=>{

    let component:LoginComponent;
    let fixture:ComponentFixture<LoginComponent>;

    beforeEach(async()=>{
        TestBed.configureTestingModule({
            declarations:[LoginComponent]
        }).compileComponents();
    });

    beforeEach(() => {
        fixture = TestBed.createComponent(LoginComponent);
        component = fixture.componentInstance;
        fixture.detectChanges();
    });

但是,立即,uni测试被打破如下:

[ERROR ->]<mat-list-item role="listitem">
            <button (click)="login()" mat-raised-button color="primary"  "): ng:///DynamicTestModule/LoginComponent.html@23:4
    'mat-list' is not a known element:
    1. If 'mat-list' is an Angular component, then verify that it is part of this module.
    2. If 'mat-list' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. ("

我试着按照指令进入ng_module,但没用,测试仍然没法。

请帮忙建议如何摆脱这个错误?

由于

1 个答案:

答案 0 :(得分:0)

当您测试任何component时,必须在测试规范文件中导入所有必需的modules,这些component由被测import使用。

我建议您在测试规范中angular-material添加{{1}}新添加的模块。