我如何在 Rust 中测试私有(private)方法?我没有找到任何关于它的信息。文档中也没有任何信息。
最佳答案
使用 #[test]
时,私有(private)或公共(public)方法没有什么特别之处——您只是在编写可以访问它们可以访问的任何内容的完美正常函数。
fn private_function() {
}
#[test]
fn test_private_function() {
private_function()
}
外部测试,例如 tests/*.rs
和 examples/*.rs
如果您使用的是 Cargo 或 doc 测试,则无法访问私有(private)成员;他们也不应该:此类测试旨在成为公共(public) API 测试,而不是处理实现细节。
关于testing - 我如何在 Rust 中测试私有(private)方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27978377/