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(const TypeMatcher<MyCustErr>()));
expect(() => operations.lookupOrderDetails(), isInstanceOf<MyCustErr>());