Angular: How to mock MatDialogRef while testing

前端 未结 4 2254
隐瞒了意图╮
隐瞒了意图╮ 2021-02-18 13:56

I have a DialogComponent that has the following constructor where Dialog is a custom object:

constructor(
    public dialogRef: MatDialogRef

        
4条回答
  •  星月不相逢
    2021-02-18 14:44

    If you use at least one MatDialogRef method, you should create a mock. For example I use the close() method. Without it errors would be generated so I made the below class with an empty method.

    export class MatDialogRefMock {
        close(value = '') {
    
        }
    }
    

    and use that instead of an empty value, with useClass

    { provide: MatDialogRef, useClass: MatDialogRefMock },
    

提交回复
热议问题