我目前正在开发一个使用 bloc 架构的应用程序。我的 Bloc 专门使用流与 UI 进行通信。因此,除了构造函数之外的所有方法都是私有(private)的(它们以'_'开头)。
所以问题是我如何从文本包中的测试类测试 bloc 的私有(private)方法,使其无法访问其他包的私有(private)方法。
最佳答案
你不能,但你可以将它们公开并且
使用 @visibleForTesting
对其进行注释,以在从不在同一库或 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.
https://stackoverflow.com/questions/54219835/