How to test private functions/methods in Flutter? How to test private functions/methods in Flutter? flutter flutter

How to test private functions/methods in Flutter?


You can't, but you can make them public and annotate it with @visibleForTesting to get an DartAnalyzer warning when they are accessed from code that is not in in the same library or in 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.


I solved this problem right now by making another public 'stub' method with the same parameters that just calls the private one and marked it with @visibleForTesting. This

@visibleForTesting  Future<void> removeAppointments(List<DocumentSnapshot> documents, [FirebaseFirestore instance]) => _removeAppointments(documents, instance);