简体   繁体   English

颤动:测试窗口小部件测试中的异常

[英]Flutter: Testing for exceptions in widget tests

How do I go about making sure the ui (widget) throws an exception during widget testing in Flutter. 我如何确保ui(小部件)在Flutter中的小部件测试期间抛出异常。 Here is my code that does not work: 这是我的代码不起作用:

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

It fails with the following error 它失败并出现以下错误

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

OR......should I be testing how the UI handles an exception by looking for error messages, etc?? 或者......我应该通过查找错误消息等来测试UI如何处理异常?

To catch exceptions thrown in a flutter test, use WidgetTester.takeException . 要捕获flutter测试中抛出的异常,请使用WidgetTester.takeException This returns the last exception caught by the framework. 这将返回框架捕获的最后一个异常。

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

You also don't need a throwsA matcher, since it is not being thrown from the method. 您也不需要throwsA匹配器,因为它不会从方法中抛出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM