我只是在单元测试组件是否已创建。以下是我的规范文件
import { async, ComponentFixture, TestBed, fakeAsync, tick } from '@angular/core/testing';
import { CUSTOM_ELEMENTS_SCHEMA, DebugElement } from '@angular/core';
import { RouterTestingModule } from '@angular/router/testing';
import { MatTableModule } from '@angular/material';
import { HttpClientModule, HttpXhrBackend } from '@angular/common/http';
import { MockBackend } from '@angular/http/testing';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { ReleaseListComponent } from './release-list.component';
import { ReleaseService } from '../release.service';
describe('ReleaseListComponent', () => {
let component: ReleaseListComponent;
let fixture: ComponentFixture<ReleaseListComponent>;
beforeEach(() => {
TestBed.configureTestingModule({
schemas: [CUSTOM_ELEMENTS_SCHEMA],
imports: [RouterTestingModule, MatTableModule, HttpClientModule ],
declarations: [ReleaseListComponent],
providers: [ReleaseService]
})
.compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(ReleaseListComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeDefined();
});
我的组件
import { Component, OnInit, ViewChild } from '@angular/core';
import { ReleaseService } from '../release.service';
import { MatPaginator, MatSort, MatTableDataSource } from '@angular/material';
@Component({
selector: 'bw-release-list',
templateUrl: './release-list.component.html'
})
export class ReleaseListComponent implements OnInit {
title = 'Release notes';
displayedColumns = ['title'];
dataSource;
@ViewChild(MatPaginator) paginator: MatPaginator;
@ViewChild(MatSort) sort: MatSort;
constructor( private releaseNotes: ReleaseService ) { }
ngOnInit() {
this.releaseNotes.getReleaseNotes()
.subscribe(data => {
this.dataSource = new MatTableDataSource(data.results);
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
});
}
applyFilter(filterValue: string) {
filterValue = filterValue.trim();
filterValue = filterValue.toLowerCase();
this.dataSource.filter = filterValue;
}
}
我一直在调试,但是出现了An error was thrown in afterAll
的错误,已经尝试了很多方法,包括在async
和sync
中使用beforeEach
,也将所有依赖文件导入到规范文件中,但仍然出现相同的错误。
请建议我如何解决这个问题。