Flutter: Testing for exceptions in widget tests

前提是你 提交于 2020-01-02 04:47:11

问题


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

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??


回答1:


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

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



来源:https://stackoverflow.com/questions/54262024/flutter-testing-for-exceptions-in-widget-tests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!