【发布时间】:2019-03-22 22:09:55
【问题描述】:
我只是对组件是否已创建进行单元测试。以下是我的规范文件
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,我还把所有的依赖文件都导入了spec 文件。仍然抛出相同的错误。
请建议我如何解决此问题。
【问题讨论】:
标签: angular jasmine karma-jasmine