有没有一种方法可以测试构建抛出异常的小部件。
我在testWidgets
中尝试过,但它无法识别错误。
expectLater(() async {
await tester.pumpWidget(...);
}, throwsException);
您只能捕获由代码调用的代码引发的异常build()
由Flutter框架调用,因此出现错误。
您可以在测试中为此类异常注册自定义错误处理程序
final errorHandled = expectAsync0((){});
FlutterError.onError = (errorDetails) {
// handle error
errorHandled();
});
这样,如果在测试超时之前未调用errorHandled
,则测试将失败。
https://docs.flutter.io/flutter/foundation/FlutterError-class.html