如何测试打开MatDialog的组件

时间:2018-02-17 14:28:57

标签: angular

我有一个成功登录时打开的MatDialog组件。 onSignin函数调用openDialog函数。此处对话框的类型为MatDialog

openDialog(statusCode: number, dialogData: Dialog) {
  const dialogRef = this.dialog.open(CustomDialogComponent, { data: dialogData })

  if (statusCode === 200) {
    dialogRef.disableClose = true
  }
}

以下是测试:

it('ask to log out if no parentUrl is found on signin', () => {
  const user = {
    id: '123'
  }


  spyOn(component.dialog, 'open')
  component.onSignin(user)
  fixture.detectChanges()

  const dialogData = new Dialog()
  dialogData.title = 'Logged In'
  dialogData.message = 'Your signin is successful.'
  dialogData.buttonTextPrimary = 'Log Out'
  dialogData.alertType = 'success'

  expect(component.dialog.open).toHaveBeenCalledWith(CustomDialogComponent, {data : dialogData})
})

我收到以下错误:

TypeError: Cannot set property 'disableClose' of undefined

这里的dialogRef是未定义的。怎么解决这个问题?

0 个答案:

没有答案