我有一个使用 Angular Material 呈现表格的组件mat-table。它工作正常,但在我的规范文件中,该表没有呈现正文的元素(tds 和trs),因此测试失败。似乎我正确初始化了文本,我导入了MatTableModule,并且还初始化了mat-table.
组件获取输入到数据源,它不是异步的,我fixture.detectChanges()在初始化输入后调用。
table和body标签被渲染,但标签body是空的。table为什么没有呈现数据的任何建议?
谢谢
这是测试代码的示例:
let component: MyComponent;
let fixture: ComponentFixture<MyComponent>;
const dataSource: DataSource[] = [{id: 1, name: 'a'}];
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations:[MyComponent],
imports: [MatTableModule],
}));
beforeEach(() => {
fixture = TestBed.createComponent(MyComponent);
component = fixture.componentInstance;
debugElement = fixture.debugElement
});
it('should display results', () => {
component.myDataSource = dataSource;
fixture.detectChanges();
console.log(debugElement.nativeElement); //here I see that the body is empty
const elements = debugElement.queryAll(By.css('td'));
expect(elements.length).toBe(2); // I get 0
});