testing - 如何在 Flutter 中测试私有(private)函数/方法?

我目前正在开发一个使用 bloc 架构的应用程序。我的 Bloc 专门使用流与 UI 进行通信。因此,除了构造函数之外的所有方法都是私有(private)的(它们以'_'开头)。

所以问题是我如何从文本包中的测试类测试 bloc 的私有(private)方法,使其无法访问其他包的私有(private)方法。

最佳答案

你不能,但你可以将它们公开并且 使用 @visibleForTesting 对其进行注释,以在从不在同一库或 test/

中的代码访问它们时获得 DartAnalyzer 警告

https://github.com/dart-lang/sdk/blob/master/pkg/meta/lib/meta.dart#L224-L233

/// Used to annotate a declaration was made public, so that it is more visible
/// than otherwise necessary, to make code testable.
///
/// Tools, such as the analyzer, can provide feedback if
///
/// * the annotation is associated with a declaration not in the `lib` folder
///   of a package, or
/// * the declaration is referenced outside of its the defining library or a
///   library which is in the `test` folder of the defining package.

https://stackoverflow.com/questions/54219835/

相关文章:

dart - 事件队列和微任务队列

flutter - 如何在 Flutter 中向 TextFormField 添加阴影

dart - 同屏多于一个 float 操作按钮显示黑屏

android - Flutter:构建 APK 时如何传递 Gradle 参数?

dart - Flutter:StatelessWidget.build 被多次调用

dart - 如何在 Dart 中发出网络请求并返回一个 json 对象

dart - flutter ListView 滚动到索引不可用

dart - Flutter - 如何在 flutter 的数字键盘中添加完成按钮

dart - Flutter ThemeData 不适用于文本

amazon-web-services - 在 Flutter 中从 AWS S3 上传和获取媒体文