dart - Flutter:测试是否抛出了特定的异常
问题描述
简而言之,throwsA(anything)
在 dart 中进行单元测试时,这对我来说是不够的。如何测试特定的错误消息或类型?
这是我想捕捉的错误:
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>()));
如果您只想检查异常,请检查此答案:
推荐阅读
- python - 图像分类,网络没有学习任何东西
- jenkins - 在 Jenkins 流水线库中使用 withCredentials([usernamePassword( ... ) ])
- laravel - spatie/multi-tenancy - 对房东管理面板无效的目录名称使用不同的保护
- picat - 如何在 Picat 中构建格雷码生成器?
- stackexchange.redis - 在 StackExchange.Redis 回调中使用服务
- css - 固定高度表,带有溢出-x 用于单元格内容
- javascript - 延迟加载样式=“跨度标签内的背景图像”
- python - Raspberry Pi CSI 摄像头到 Python 中的 USB 网络摄像头
- mongodb - 从 Spark 自定义 Mongo 中的写入操作
- triggers - 在同一张表上插入后更新的 MYSQL 触发器(更新应该基于另一个表的条件)