dart - Flutter:测试是否抛出了特定的异常

简而言之,在 dart 中进行单元测试时,throwsA(anything) 对我来说是不够的。如何测试特定的错误消息或类型

这是我想捕捉的错误:

class MyCustErr implements Exception {
  String term;

  String errMsg() => 'You have already added a container with the id 
  $term. Duplicates are not allowed';

  MyCustErr({this.term});
}

这是当前通过的断言,但想检查上面的错误类型:

expect(() => operations.lookupOrderDetails(), throwsA(anything));

这就是我想做的:

expect(() => operations.lookupOrderDetails(), throwsA(MyCustErr));

最佳答案

这应该做你想做的:

expect(() => operations.lookupOrderDetails(), throwsA(isA<MyCustErr>()));

如果您只想检查异常,请查看 answer :

https://stackoverflow.com/questions/54241396/

相关文章:

flutter - 如何在 flutter 中改变英雄动画的速度

android - 在 dart/flutter 项目中包含 C 文件

android - 路由后,TextField 单击重建/重新加载小部件

listview - flutter listView.Builder 隐藏最后一个列表项的分隔符

Flutter - 屏幕的多个文件?

android - Flutter 不选择基于 fontWeight 的自定义字体

dart - Flutter 能否免去使用 mac 来创建 IOS 应用程序?

firebase - Flutter 中的 cloud_firestore 和 firebase_a

dart - Flutter popUntil 抛出Bad state : Future alrea

flutter - 如何使 Flutter RaisedButton 处于选中状态