10

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

4

1 回答 1

6

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

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

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

于 2019-01-18T22:10:31.097 回答