如何在 Flutter 中测试私有函数/方法? [英] How to test private functions/methods in Flutter?

查看:37
本文介绍了如何在 Flutter 中测试私有函数/方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm currently developing an app that uses the bloc architecture. My bloc is using streams exclusively to communicate with the UI. Therefore all its methods except for the constructor are private ( they start by '_').

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

So the question is how can I test the bloc's private methods from the test class that lives in the text package so it cannot access private methods of other packages.

谢谢

推荐答案

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

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.

这篇关于如何在 Flutter 中测试私有函数/方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆