单元测试Angular Material对话框 - 如何包含MAT_DIALOG_DATA

26
我正在尝试对这个材料对话框进行单元测试,以测试模板是否渲染了正确的注入对象。当组件被正确使用时,它可以正常工作。
组件 - 对话框
export class ConfirmationDialogComponent {

  constructor(@Inject(MAT_DIALOG_DATA) private dialogModel: ConfirmationDialogModel) {}
}

对话框模板

<h1 mat-dialog-title *ngIf="dialogModel.Title">{{dialogModel.Title}}</h1>
<div mat-dialog-content>
  {{dialogModel.SupportingText}}
</div>
<div mat-dialog-actions>
  <button mat-button color="primary" [mat-dialog-close]="false">Cancel</button>
  <button mat-raised-button color="primary"[mat-dialog-close]="true" cdkFocusInitial>{{dialogModel.ActionButton}}</button>
</div>

模型 - 注入内容是什么

export interface ConfirmationDialogModel {
  Title?: string;
  SupportingText: string;
  ActionButton: string;
}

单元测试 - 我在哪里遇到了问题

describe('Confirmation Dialog Component', () => {

  const model: ConfirmationDialogModel = {
    ActionButton: 'Delete',
    SupportingText: 'Are you sure?',
  };

  let component: ConfirmationDialogComponent;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [
        ConfirmationDialogComponent
      ],
      imports: [
        MatButtonModule,
        MatDialogModule
      ],
      providers: [
        {
          // I was expecting this will pass the desired value
          provide: MAT_DIALOG_DATA,
          useValue: model
        }
      ]
    });

    component = TestBed.get(ConfirmationDialogComponent);
  }));

  it('should be created', async(() => {
    expect(component).toBeTruthy();
  }));
});

Karma错误

Karma Error Screenshot

2个回答

42

试试这个:

describe('Confirmation Dialog Component', () => {
    
  const model: ConfirmationDialogModel = {
    ActionButton: 'Delete',
    SupportingText: 'Are you sure?',
  };
    
  let component: ConfirmationDialogComponent;
  let fixture: ComponentFixture<ConfirmationDialogComponent>;
    
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [
        ConfirmationDialogComponent
      ],
      imports: [
        MatButtonModule,
        MatDialogModule
      ],
      providers: [
        {
          // I was expecting this will pass the desired value
          provide: MAT_DIALOG_DATA,
          useValue: model
        }
      ]
    })
      .compileComponents();
            
  }));
    
        
  beforeEach(() => {
    fixture = TestBed.createComponent(ConfirmationDialogComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });
    
  it('should be created', async(() => {
    expect(component).toBeTruthy();
  }));

  it('should close dialog when close button clicked', fakeAsync(() => {
    component.onCloseButtonClicked(0);
    fixture.detectChanges();
    tick();
    expect(mockMainDialogRef.close.calls.count()).toBe(1, 'dialog closed');
  }));
});

12
此外,如果您在组件中使用 MatDialogRef,则需要将其包含在 providers 数组中。示例:providers: [{ provide: MatDialogRef, useValue: { close: (dialogResult: any) => { } } }]来源 - ndraiman
我需要如何测试类似“应该关闭”的东西? - utdev
@utdev 我已经更新了答案,加入了应该关闭的单元测试。 - Indrakumara
6
@Indrakumara,您确实更新了答案,但是mockMainDialogRef从何而来并不明显。如何获取该引用? - Elias

9
这里是一个关于如何在单元测试中注入MAT_DIALOG_DATA的例子:
 import { async, ComponentFixture, TestBed } from '@angular/core/testing';
 import { MatDialogModule, MAT_DIALOG_DATA } from '@angular/material/dialog';

 import { ConfirmDialogComponent } from './confirm-dialog.component';

 describe('ConfirmDialogComponent', () => {
   let component: ConfirmDialogComponent;
   let fixture: ComponentFixture<ConfirmDialogComponent>;

   beforeEach(async(() => {
     TestBed.configureTestingModule({
       declarations: [ ConfirmDialogComponent ],
       imports: [ MatDialogModule ], // add here
       providers: [
        { provide: MAT_DIALOG_DATA, useValue: {} } // add here
      ],
    })
    .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(ConfirmDialogComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接