如何使用茉莉花和 Angular 7 为垫子对话框编写单元测试?



我正在为打开mat对话框的组件中的方法编写单元测试。尝试编写以下单元测试以确保对话框是否打开。但是我收到一个错误。

组件.ts:

它具有上传方法。

onUpload(event: FileUpload) {
this.Service
.get(event.id)
.subscribe((data: Data[]) => {
const dialogRef = this.dialog.open(
DialogComponent,
{data,}    
);
dialogRef.afterClosed().subscribe({
next: (Id: string) => {
if (Id === null {
this.reset();
}

unittest.spec.ts:

describe('open()', () => {
it('should open the dialog', () => {
const testCases = [
{
returnValue: 'Successfully opens a dialog',
isSuccess: true
},
{
returnValue: 'cancel',
isSuccess: false
},
];
testCases.forEach(testCase => {
it(`should open the file upload matDialog `, () => {
const returnedVal = {
afterClosed: () => of(testCase.returnValue)
};
spyOn(component, 'reset');
spyOn(component['matDialog'], 'open').and.returnValue(returnedVal);
component.onUpload(new FileUpload(
'fid',
'oid',
));
if (testCase.isSuccess) {
expect(component.reset).toHaveBeenCalled();
} else {
expect(component.reset).not.toHaveBeenCalled();
}
expect(component['matDialog'].open).not.toHaveBeenCalled();
});
});

我收到一个错误" 'mat-grid-tile' 不是已知元素: 1. 如果"mat-grid-tile"是 Angular 组件,请验证它是否是该模块的一部分。请帮忙。谢谢。

"错误:'it'应该只在'描述'函数中使用">与嵌套it函数有关。如果删除封闭it语句,则应删除该错误。

describe('open()', () => {
// it('should open the dialog', () => { // get rid of this
const testCases = [
...

最新更新