首页 文章

Angular2材质'md-icon'不是Karma / Jasmine的已知元素

提问于
浏览
10

我正在使用@ angular / material 2.0.0-alpha.11-3 angular-cli 1.0.0-beta.19-3 karma 1.2.0 karma-jasmine 1.0.2开发Angular2应用程序

运行它工作正常,但模板有md-icon按钮的几个测试失败,模板错误:

ERROR: 'Unhandled Promise rejection:', 'Template parse errors:
'md-icon' is not a known element:
1. If 'md-icon' is an Angular component, then verify that it is part of this module.                                
2. If 'md-icon' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schemas' of this component to suppress this message

我的app.module.ts:

import { MaterialModule } from '@angular/material';

@NgModule({
  declarations: [
    AppComponent,
    LinearProgressIndicatorComponent,
    MyNewDirectiveDirective,
    MyNewServiceDirective,
    HeaderComponent,
    MenuComponent,
    WatchpanelComponent,
    InputComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    NgbModule.forRoot(),
    MaterialModule.forRoot(),
  ],
  exports: [ MaterialModule ],
  providers: [LocalStorage],
  bootstrap: [AppComponent]
})
export class AppModule { }

watchpanel.component.spec.ts:

import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { DebugElement } from '@angular/core';
import { WatchpanelComponent } from './watchpanel.component';

describe('WatchpanelComponent', () => {
  let component: WatchpanelComponent;
  let fixture: ComponentFixture<WatchpanelComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ WatchpanelComponent ] // declare the test component
    })
    .compileComponents();

    fixture = TestBed.createComponent(WatchpanelComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();

  }));

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

  it('should create', () => {
    expect(component).toBeTruthy();
  });
});

据我所知,@ angular / material现在包含导入所需的唯一模块,MaterialModule . 我尝试从@ angular2-material / icon导入MdIconModule但没有成功 . 我究竟做错了什么 ?提前致谢

3 回答

  • 14

    按照yurzui的建议导入MaterialModule并在返回promise后创建组件解决了它 . 谢谢yurzui

    import { async, ComponentFixture, TestBed } from '@angular/core/testing';
    import { By } from '@angular/platform-browser';
    import { DebugElement } from '@angular/core';
    import { WatchpanelComponent } from './watchpanel.component';
    import { FormsModule } from '@angular/forms';
    import { MaterialModule } from '@angular/material';
    
    describe('WatchpanelComponent', () => {
      let component: WatchpanelComponent;
      let fixture: ComponentFixture<WatchpanelComponent>;
    
      beforeEach(async(() => {
        TestBed.configureTestingModule({
          imports: [ MaterialModule.forRoot() ],
          // forRoot() deprecated
          // in later versions ------^
          declarations: [ WatchpanelComponent ] // declare the test component
        })
        .compileComponents().then(() => {
          fixture = TestBed.createComponent(WatchpanelComponent);
          component = fixture.componentInstance;
          fixture.detectChanges();
        });
    
      }));
    
      // beforeEach(() => {
      //   fixture = TestBed.createComponent(WatchpanelComponent);
      //   component = fixture.componentInstance;
      //   fixture.detectChanges();
      // });
    
      it('should create', () => {
        expect(component).toBeTruthy();
      });
    });
    
  • 4

    事情've changed in newer versions of Angular Material since @javahaxxor'的回答 . 我已经解决了这个问题,导入了相同的模块,就像我在 AppModule 中一样(我也需要这里的表格):

    import {
      MatButtonModule,
      MatCardModule,
      MatIconModule,
      MatInputModule,
      MatProgressSpinnerModule,
      MatDialogModule,
      MatCheckboxModule
    } from '@angular/material';
    
    // ... not important
    
    beforeEach(async(() => {
        TestBed.configureTestingModule({
          declarations: [ WelcomeComponent ],
          imports: [
            NoopAnimationsModule,
            FormsModule,
            ReactiveFormsModule,
            MatButtonModule,
            MatCardModule,
            MatIconModule,
            MatInputModule,
            MatProgressSpinnerModule,
            MatDialogModule,
            MatCheckboxModule
          ],
          providers: [
            // ...
          ]
        })
        .compileComponents();
      }));
    
  • 2

    最新版本的Angular Material中不再提供md-icon . 所有标签/元素现在都以"mat"为前缀而不是"md" ..所以 <md-icon> ..成为.. <mat-icon>

相关问题