Angular 2单元测试 - <component>虽然已声明但不是已知元素

时间:2017-08-22 09:22:04

标签: angular unit-testing

我在测试用于显示错误消息的组件时遇到错误 该组件有一个横幅,编译器无法识别。

我使用存根而不是普通的横幅组件,因为它使用了一些独立于测试的其他东西。但是,使用普通的横幅组件会产生相同的错误。

error.component.html

//imports

describe('ErrorComponent', () => {
    let fixture: ComponentFixture<ErrorComponent>;
    let app;
    let mockRouter = {
        navigate: jasmine.createSpy('navigate')
    }; 
    let activatedRoute = {};
    let location = {

    };

    beforeEach(async(() => {

            TestBed.configureTestingModule({
                imports: [MdInputModule, MdDialogModule, RouterTestingModule, MdButtonModule, MdCardModule, MdMenuModule, MdToolbarModule, MdIconModule, MdCheckboxModule, MdInputModule, MdListModule, MdAutocompleteModule, MdSelectModule, MdTooltipModule, MdSidenavModule, MdGridListModule, MdSnackBarModule],
                declarations: [BannerStubComponent, ErrorComponent], // declare the test component
                providers: [{provide: ActivatedRoute, useValue: activatedRoute}, {provide: Router, useValue: mockRouter}, {provide: Location, useValue: location}]
            })
                .compileComponents();  // compile template and css

            fixture = TestBed.createComponent(ErrorComponent);
            app = fixture.debugElement.componentInstance;
     }));

     //tests
})

error.component.spec.ts

import {Component} from '@angular/core';
@Component({
    selector: 'banner-component',
    template: ``
})
export class BannerStubComponent {

}

bannerstub.component.ts

'banner-component' is not a known element:
1. If 'banner-component' is an Angular component, then verify that it is part of this module.
2. If 'banner-component' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. ("[ERROR ->]<banner-component></banner-component>
<md-card class="card">

输出

NO_ERRORS_SCHEMA

到目前为止,我已尝试按照Angular 2 Testing Documentation的建议向NgModule添加{{1}},但没有成功(产生相同的错误)。

Stub组件声明正确,我不确定我在这里缺少什么。

0 个答案:

没有答案