testing - 如何在 Flutter 中测试私有函数/方法?
问题描述
我目前正在开发一个使用 bloc 架构的应用程序。我的集团专门使用流与 UI 进行通信。因此,除了构造函数之外的所有方法都是私有的(它们以'_'开头)。
所以问题是我如何从文本包中的测试类测试集团的私有方法,这样它就不能访问其他包的私有方法。
谢谢
解决方案
你不能,但你可以将它们公开并注释它,@visibleForTesting
以便在从不在同一个库中或不在同一库中的代码访问它们时获得 DartAnalyzer 警告test/
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.
推荐阅读
- react-native - createBottomTabNavigator 已移至错误消息
- python - 避免达到速率限制
- javascript - JavaScript - 对象无法访问输入的值
- javascript - 带有 React 和 Redux 的收藏夹购物车
- python - 如何将参与者分配给研究?
- python - 快速加入两个 DataFrame
- docker - nvidia Jetson Nano 上的 Docker 未执行
- c - sizeof() 具有零长度数组成员的结构
- charts - 如何检查舵图中是否存在逗号?
- android - 通过 FTP 将 .mp3 文件下载到我的应用程序会更改文件的质量