一尘不染

Flutter:测试是否抛出特定异常

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));


阅读 394

收藏
2020-08-13

共1个答案

一尘不染

这应该做您想要的:

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

expect(() => operations.lookupOrderDetails(), isInstanceOf<MyCustErr>());
2020-08-13