jsa*_*ter 10 karma-jasmine angular
我正在使用@ 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
Run Code Online (Sandbox Code Playgroud)
我的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 { }
Run Code Online (Sandbox Code Playgroud)
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();
});
});
Run Code Online (Sandbox Code Playgroud)
据我所知,@ angular/material现在包含导入所需的唯一模块,MaterialModule.我尝试从@ angular2-material/icon导入MdIconModule但没有成功.我究竟做错了什么 ?提前致谢
jsa*_*ter 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();
});
});
Run Code Online (Sandbox Code Playgroud)
自@ javahaxxor回答以来,Angular Material的新版本已经发生了变化.我已经通过导入相同的模块解决了这个问题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();
}));
Run Code Online (Sandbox Code Playgroud)
md-icon在最新版本的Angular Material中不再可用。现在,所有标签/元素都以“ mat”而不是“ md”作为前缀。..因此<md-icon>成为..<mat-icon>
| 归档时间: |
|
| 查看次数: |
11919 次 |
| 最近记录: |