如何在 Rust 中测试私有方法? [英] How do I test private methods in Rust?

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

问题描述

如何在 Rust 中测试私有方法?我没有找到任何关于它的信息.文档中也没有信息.

How do I test private methods in Rust? I didn't find any information about it. There's no information in the documentation either.

推荐答案

当使用 #[test] 时,私有或公共方法没有什么特别之处——你只是编写完全正常的函数,可以访问他们可以访问的任何内容.

When using #[test], there’s nothing special about private or public methods—you’re just writing perfectly normal functions that can access anything they can access.

fn private_function() {
}

#[test]
fn test_private_function() {
    private_function()
}

外部测试,例如 tests/*.rsexamples/*.rs 如果您使用 Cargo 或 doc 测试,则无法访问私有会员;他们也不应该:此类测试旨在成为公共 API 测试,而不是处理实现细节.

External tests, such as tests/*.rs and examples/*.rs if you’re using Cargo, or doc tests, do not get access to private members; nor should they: such tests are designed to be public API tests, not to be dealing with implementation details.

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

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