有人可以告诉我如何在小部件测试期间实现最重要的抖动错误,以便我可以检查自己的自定义错误。
我在网上看到一些片段提到了这一点,但是我所有的实现都失败了
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 following UnrecognizedTermException was thrown running a test:
Instance of 'UnrecognizedTermException'
...
我读到您可以执行以下代码段,但它看不到如何/在哪里实现:
final errorHandled = expectAsync0((){});
FlutterError.onError = (errorDetails) {
// handle error
errorHandled();
});
答案 0 :(得分:1)
我在生产环境中使用下面的代码将错误记录到服务器上。
main.dart:
<Image width="125" className="rounded mx-auto d-block" src="/staff_login.jpg" />
logging.dart:
import 'dart:async';
import 'package:flutter/material.dart';
import 'logging.dart';
void main() async {
FlutterError.onError = (FlutterErrorDetails details) async {
new ErrorLogger().logError(details);
};
runZoned<Future<void>>(() async {
// Your App Here
runApp(MainApp());
}, onError: (error, stackTrace) {
new ErrorLogger().log(error, stackTrace);
});
}
答案 1 :(得分:0)
这是用于测试的设计。我切换到try / catch中的包装逻辑,然后在“错误消息文本”当前概念上运行Expect()。例如:
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);