【问题标题】:Angular Unit Test MatDialog Open methodAngular 单元测试 MatDialog 打开方法
【发布时间】:2021-12-17 02:03:27
【问题描述】:

我正在尝试对这个弹出方法进行单元测试,但我不能,请你帮忙

 openPram(): void {
    const dialogConfig = new MatDialogConfig();
    this.dialog.open(ParamGameComponent, dialogConfig);
}

【问题讨论】:

    标签: angular typescript unit-testing


    【解决方案1】:

    因为这里没有很多代码,也没有说明您要测试的内容。我将假设您想测试是否使用 ParamGameComponent 的参数和 dialogConfig const 调用了 dialog.open()。如果这是真的,那么您的测试可能如下所示:

      it('open dialog test', () => {
       const openDialogSpy = spyOn(component.dialog, 'open')
       const fakeDialogConfig = new MatDialogConfig;
    
       component.openPram();
    
       expect(openDialogSpy).toHaveBeenCalledWith(ParamGameComponent, 
        fakeDialogConfig);
      });
    

    此测试中的组件 var 还假设您拥有默认的 Angular CLI 生成的 spec.ts 文件,该文件会创建您正在为其编写测试的组件的本地实例。

    【讨论】:

    • 谢谢你,先生,但我只是有这个错误:'typeof ParamGameComponent' 类型的参数不可分配给'Expected 类型的参数 |未定义>'。 “typeof ParamGameComponent”类型缺少“Any”类型的以下属性:jasmineToString、asymmetricMatch
    • 抱歉我把 toHaveBeenCalledWith( ) 的参数倒过来了,答案已经更新了
    • 非常感谢它的工作原理
    猜你喜欢
    • 1970-01-01
    • 2019-03-30
    • 2020-10-14
    • 2021-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-20
    相关资源
    最近更新 更多