Flutter:测试是否抛出特定异常 [英] Flutter: Test that a specific exception is thrown
问题描述
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屋!