我目前正在开发使用bloc架构的应用。我的集团专门使用流与UI进行通信。因此,除构造函数外,它的所有方法都是私有的(它们以“ _”开头)。
所以问题是我该如何从位于文本包中的测试类中测试块的私有方法,以使其无法访问其他包的私有方法。
谢谢
答案 0 :(得分:1)
您不能,但是可以将其公开并
使用@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.
答案 1 :(得分:1)
我现在通过使另一个公共“存根”方法具有相同的参数来解决此问题,该方法仅调用私有变量并用@visibleForTesting
进行标记。这个
@visibleForTesting
Future<void> removeAppointments(List<DocumentSnapshot> documents, [FirebaseFirestore instance]) => _removeAppointments(documents, instance);