gpt4 book ai didi

flutter - 在 Flutter testWidgets 小部件测试中运行时,Dart `File.writeAsString` 或 `File.writeAsBytes` 挂起

转载 作者:行者123 更新时间:2023-12-05 02:42:18 28 4
gpt4 key购买 nike

File.writeAsString 当我尝试在小部件测试中运行时挂起(我的目标是在测试失败时使用重绘边界截取被测小部件的屏幕截图并将 png 保存到文件中,就像运行此命令更新黄金文件一样)

  testWidgets("Display correct greeting text and user firstname morning start",
(WidgetTester _tester) async {
print("Saving test widget");

await File("test_generated_4.txt").writeAsString('some content');
// code never reaches here
print("Saved test widget");
});

最佳答案

找到了

  testWidgets("Display correct greeting text and user firstname morning start",
(WidgetTester _tester) async {
await _tester.runAsync(() async {
print("Saving test widget");

await File("test_generated_4.txt").writeAsString('some content');
// await writeToFile(byteDataAll, './test_generated.png');
print("Saved test widget");
});
});

来自文档:

  /// Runs a [callback] that performs real asynchronous work.
///
/// This is intended for callers who need to call asynchronous methods where
/// the methods spawn isolates or OS threads and thus cannot be executed
/// synchronously by calling [pump].
///
/// If callers were to run these types of asynchronous tasks directly in
/// their test methods, they run the possibility of encountering deadlocks.
///
/// If [callback] completes successfully, this will return the future
/// returned by [callback].
///
/// If [callback] completes with an error, the error will be caught by the
/// Flutter framework and made available via [takeException], and this method
/// will return a future that completes with `null`.
///
/// Re-entrant calls to this method are not allowed; callers of this method
/// are required to wait for the returned future to complete before calling
/// this method again. Attempts to do otherwise will result in a
/// [TestFailure] error being thrown.
///
/// If your widget test hangs and you are using [runAsync], chances are your
/// code depends on the result of a task that did not complete. Fake async
/// environment is unable to resolve a future that was created in [runAsync].
/// If you observe such behavior or flakiness, you have a number of options:
///
/// * Consider restructuring your code so you do not need [runAsync]. This is
/// the optimal solution as widget tests are designed to run in fake async
/// environment.
///
/// * Expose a [Future] in your application code that signals the readiness of
/// your widget tree, then await that future inside [callback].
Future<T?> runAsync<T>(

关于flutter - 在 Flutter testWidgets 小部件测试中运行时,Dart `File.writeAsString` 或 `File.writeAsBytes` 挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67685818/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com