Flutter:如何正确实现FlutterError.OnError [英] Flutter: How to implement FlutterError.OnError correctly
问题描述
有人可以告诉我如何在小部件测试期间实现最重要的抖动错误,以便我可以检查自己的自定义错误.
Can someone show me how to implement overriding flutter errors during widget test so I can check for my own custom errors.
我在网上看到一些片段提到了这一点,但是我所有的实现都失败了
I have seen snippets online mentioning this but all of my implementations fail
void main() {
testWidgets('throws an error when scanning unknown term types', (WidgetTester tester) async {
await tester.pumpWidget(injectTestWidget(new ItemScanScreen()));
await tester.enterText(find.byKey(new Key('term')), '');
await tester.tap(find.byIcon(Icons.send));
await tester.pump();
expect(
tester.takeException(),
isInstanceOf<UnrecognizedTermException>(),
reason: 'should have thrown an UnrecognizedTermException error but didn\'t',
);
});
}
即使看起来实际上抓住"了我的错误,上面的代码也会失败,并显示以下消息:
the code above fails with the message below even though it looks like it in fact 'caught' my error:
The following UnrecognizedTermException was thrown running a test:
Instance of 'UnrecognizedTermException'
...
我读到您可以执行下面的代码片段,但是它看不到如何/在何处实现它:
I read that you could do something like the snippet below but it did not see how/where to implement it:
final errorHandled = expectAsync0((){});
FlutterError.onError = (errorDetails) {
// handle error
errorHandled();
});
推荐答案
这是用于测试的设计.我切换到try/catch中的包装逻辑,然后在错误消息文本"当前概念上运行Expect().例如:
This is was design for a test. I switched to wrapping logic in try/catch then running expect() on the "error message text" present concept. ex:
try {
throw new UnrecognizedTermException();
} catch (e) {
setState(() => _status = e.errMsg());
}
// then in my test
expect(find.text('Could not determine the type of item you scanned'), findsOneWidget);
这篇关于Flutter:如何正确实现FlutterError.OnError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!