Flutter:测试是否抛出特定异常 [英] Flutter: Test that a specific exception is thrown

查看:211
本文介绍了Flutter:测试是否抛出特定异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简而言之, throwsA(任何东西)对我来说在飞镖单元测试中是不够的。我如何测试特定的错误消息或类型



这是我想捕获的错误:

 类MyCustErr实现了异常{
字符串条件;

字符串errMsg()=> ’您已经添加了一个ID为
$ term的容器。不允许重复’;

MyCustErr({this.term});
}

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



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



这就是我想做的:



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

解决方案

想要:

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

Expect(()=> Operations.lookupOrderDetails(),isInstanceOf< MyCustErr>());

如果您只想检查异常,请检查此答案


in short, throwsA(anything) does not suffice for me while unit testing in dart. How to I test for a specific error message or type?

Here is the error I would like to catch:

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

here is the current assertion that passes, but would like to check for the error type above:

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

This is what I want to do:

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

解决方案

This should do what you want:

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

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

if you just want to check for exception check this answer:

这篇关于Flutter:测试是否抛出特定异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆