如何在 Flutter 中测试私有函数/方法?

编程入门 行业动态 更新时间:2024-10-09 00:48:39
本文介绍了如何在 Flutter 中测试私有函数/方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我目前正在开发一个使用 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/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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 22:35:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1400897.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   测试   方法   如何在   Flutter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!