Flutter:在小部件测试中测试异常
问题内容:
如何确保在Flutter中的小部件测试期间ui(小部件)引发异常。这是我的代码不起作用:
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如何 处理 异常吗?
问题答案:
要捕获抖动测试中引发的异常,请使用WidgetTester.takeException。这将返回框架捕获的最后一个异常。
await tester.tap(find.byIcon(Icons.send));
expect(tester.takeException(), isInstanceOf<UnrecognizedTermException>());
您也不需要throwsA
匹配器,因为它不会从方法中抛出。