在Angular 2中进行测试时,何时在TestBed中使用异步函数?
你什么时候用的?
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [MyModule],
schemas: [NO_ERRORS_SCHEMA],
});
});
Run Code Online (Sandbox Code Playgroud)
你何时使用它?
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [MyModule],
schemas: [NO_ERRORS_SCHEMA],
});
}));
Run Code Online (Sandbox Code Playgroud)
任何人都可以启发我吗?
unit-testing karma-jasmine angular2-testing angular angular-test
我有一个来自端点的数据并将其放入 MatTableDataSource。我能够让它为 MatSort 和 MatPaginator 工作,但需要使用 setTimeOut,这似乎不是执行此操作的正确方法。如果我删除它,它会抱怨“无法读取未定义类型的属性”,我认为这是因为它尚未初始化。
我也试过:
这是我当前的代码(正在运行,但使用 settimeout)
<div *ngIf="!isLoading">
<div *ngFor="let record of renderedData | async" matSort>
// ... some html to show the 'record'
<mat-paginator #paginator
[pageSizeOptions]="[5, 10, 20]">
</mat-paginator>
</div>
Run Code Online (Sandbox Code Playgroud)
组件
export class ResultsComponent implements OnInit, OnDestroy, AfterViewInit {
dataSource: MatTableDataSource<any> = new MatTableDataSource();
renderedData: Observable<any>;
@ViewChild(MatPaginator) paginator: MatPaginator;
@ViewChild(MatSort) sort: MatSort;
constructor(some service) {}
ngOnInit() {
const accountId = someOtherService.getAccountId();
this.someService.getData(accountId)
.subscribe((myData) => {
this.dataSource …Run Code Online (Sandbox Code Playgroud)