【发布时间】:2019-08-06 13:24:19
【问题描述】:
我对 Angular 7 有点陌生,对它的单元测试框架 Jasmine 完全陌生
所以我一直在关注 Jasmine 的文档以及一些教程。我有一个名为TestTableComponent 的组件。现在该组件有点硬编码。无论如何,我认为我面临的问题与组件本身几乎没有任何关系,所以我在这里不包括组件的代码。
我在test-table.component.spec.ts 中创建了一个测试类。代码如下:
// Required Imports have been made. Not including as unnecessary.
describe('TestTableComponent', async() =>
{
let component: TestTableComponent;
let fixture: ComponentFixture<TestTableComponent>;
let de: DebugElement;
beforeEach(async(() =>
{
TestBed.configureTestingModule({declarations:[TestTableComponent],
imports: [ReactiveFormsModule]}).compileComponents();
}));
beforeEach(() =>
{
fixture = TestBed.createComponent(TestTableComponent);
component = fixture.componentInstance;
de = fixture.debugElement;
})
it('should check if data is returned by the API')
{
const result = await component.GetEmployees;
expect(result).toBeDefined();
}
});
这里的问题是,当我执行ng test 时,它似乎运行基于此类的测试。在浏览器的控制台中,我收到一个错误(Jasmine 说1 component is pending)如下:
无法读取未定义的属性
GetEmployees。
现在,这显然意味着TestTableComponent 永远不会被初始化。我只是想知道为什么? beforeEach 没有被执行吗?如果是,那为什么component未定义?
更新:包括组件的代码
Test-table.component.ts
import { AfterViewInit, Component, OnInit, ViewChild, Inject } from '@angular/core';
import { MatPaginator, MatSort, MatTable, MatTableDataSource, MatDialogRef, Sort, ShowOnDirtyErrorStateMatcher } from '@angular/material';
import { MatDialog } from '@angular/material/dialog';
import { TestTableItem } from './test-table-datasource';
import { HttpClient, HttpParams } from '@angular/common/http';
import { UpdateModalDialogComponent } from '../update-modal-dialog/update-modal-dialog.component';
import { MessagePopUpComponent } from '../message-pop-up/message-pop-up.component';
@Component({
selector: 'app-test-table',
templateUrl: './test-table.component.html',
styleUrls: ['./test-table.component.css']
})
export class TestTableComponent implements AfterViewInit, OnInit {
@ViewChild(MatPaginator, { static: false }) paginator: MatPaginator;
@ViewChild(MatSort, { static: false }) sort: MatSort;
@ViewChild(MatTable, { static: false }) table: MatTable<TestTableItem>;
private myCollection: TestTableItem[] = [];
dataSource = new MatTableDataSource(this.myCollection);// Observable<TestTableItem>;
/** Columns displayed in the table. Columns IDs can be added, removed, or reordered. */
displayedColumns = ['id', 'fname', 'salary', 'age', 'image', 'actions'];
constructor(private http: HttpClient, private dialog:MatDialog) { }
ngOnInit() {
this.GetAllEmployees();
}
async GetAllEmployees()
{
this.dataSource = await this.GetEmployees();
}
public async GetEmployees()
{
this.myCollection = await this.http.get<TestTableItem[]>('http://localhost:22371/api/employee/GetEmployees/').toPromise();
return new MatTableDataSource(this.myCollection);
}
请注意,我没有将所有函数都包含在类中,因为这会使这篇文章变得不必要地大!
【问题讨论】:
-
你应该在测试开始时有一个
fixture.detectChanges()。第一个fixture.detectChanges()调用组件内部的onInit。不过,这可能无法完全解决您的问题。您能否也分享一下组件定义。您的组件内部是否有任何依赖项? -
将异步测试的设置移动到
asyncbeforeEach。您的函数正在一个单独的线程中竞速设置。异步调用发生在另一个线程上,所以如果beforeEach启动并且测试在之后立即开始。测试在beforeEach初始化之前就已经等待了 -
你的错误是这一行:
const result = await component.GetEmployees;。您没有包括圆括号 ()。这就是为什么编译器会抱怨“属性未定义”,即使它是一种方法。 -
@Riv,你能解释一下吗?
-
@Erbsenkoenig,是的,我的组件有依赖项。我也会分享代码,但是,我在哪里打电话
fixture.detectChanges()?在beforeEach(async()或常规beforeEach中?
标签: javascript angular jasmine