Flutter:在小部件测试中测试异常 [英] Flutter: Testing for exceptions in widget tests

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

问题描述

在Flutter中进行小部件测试时,如何确保ui(小部件)引发异常。这是我的代码不起作用:

How do I go about making sure the ui (widget) throws an exception during widget testing in Flutter. Here is my code that does not work:

expect(
  () => tester.tap(find.byIcon(Icons.send)),
  throwsA(const TypeMatcher<UnrecognizedTermException>()),
);

由于以下错误而失败

...
Expected: throws <Instance of 'TypeMatcher<UnrecognizedTermException>'>
  Actual: <Closure: () => Future<void>>
   Which: returned a Future that emitted <null>

或……我应该测试UI 如何处理通过查找错误消息等来排除异常?

OR......should I be testing how the UI handles an exception by looking for error messages, etc??

推荐答案

要捕获在闪动测试中抛出的异常,请使用 WidgetTester.takeException 。这将返回框架捕获的最后一个异常。

To catch exceptions thrown in a flutter test, use WidgetTester.takeException. This returns the last exception caught by the framework.

await tester.tap(find.byIcon(Icons.send));
expect(tester.takeException(), isInstanceOf<UnrecognizedTermException>());

您也不需要 throwsA 匹配器,因为它不是从方法中抛出的。

You also don't need a throwsA matcher, since it is not being thrown from the method.

这篇关于Flutter:在小部件测试中测试异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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