mod simulation;
use simulation::factory::FactoryType;
在
main.rs
中工作正常,但在simulation/factory.rs
内的文档测试中不起作用:
impl product_type::ProductType for FactoryType {
/// Lorem Ipsum
///
/// # Examples
///
/// ```
/// use simulation::factory::FactoryType;
///
/// ...
/// ```
fn human_id(&self) -> &String {
...
}
}
cargo test
给了我错误
---- simulation::factory::human_id_0 stdout ----
<anon>:2:9: 2:19 error: unresolved import `simulation::factory::FactoryType`. Maybe a missing `extern crate simulation`?
<anon>:2 use simulation::factory::FactoryType;
^~~~~~~~~~
error: aborting due to previous error
thread 'simulation::factory::human_id_0' panicked at 'Box<Any>', /home/rustbuild/src/rust-buildbot/slave/stable-dist-rustc-linux/build/src/libsyntax/diagnostic.rs:192
如何让文档测试发挥作用?
当您编写文档测试时,您必须充当代码的用户。给定这些文件:
src/lib.rs
pub mod simulation {
pub mod factory {
pub struct FactoryType;
impl FactoryType {
/// ```
/// use foo::simulation::factory::FactoryType;
///
/// let f = FactoryType;
/// assert_eq!(42, f.human_id())
/// ```
pub fn human_id(&self) -> u8 { 41 }
}
}
}
src/main.rs
extern crate foo;
use foo::simulation::factory::FactoryType;
fn main() {
let f = FactoryType;
println!("{}", f.human_id());
}
一切正常。请注意,在 main.rs 中,您必须说
extern crate
,然后您的所有引用都需要包含 crate 名称。文档测试是相同的,只是自动包含了 extern crate
。
正如 huon-dbaupp 所指出的,bin 板条箱无法从文档测试中导入。
解决方案是将大部分代码定义为库箱,并拥有一个二进制文件,该二进制文件只是围绕它的外壳。
例如,racer就采用了这种技术。
tldr:只需在文档测试末尾添加一行:
# fn main() {}
。
以下是我最近在编写文档测试时写的注释。它将帮助您在文档测试中使用
mod
:
这用于文档测试。
这个箱子中的一些特征需要
protobuf::MessageFull
,例如ProtoLocal: PathInfo + MessageFull
,它是为所有T: PathInfo + MessageFull
自动实现的。
要编写
ProtoLocal
文档测试,我们必须获得一个满足 Msg
和 MessageFull
特征的结构(让我们将其命名为 PathInfo
)。
但是,手动实现太难了
MessageFull
。 (MessageFull
是由著名的protobuf
生成的,它是各种RPC
的基础)。如果我们在 pub use
中生成并 my_crate
它,由于 Rust 的孤儿规则,我们不能 impl PathInfo for Msg
(我们都知道文档测试是一种集成测试)。
// in doc test
use my_crate::test_utils::Msg;
// `Msg` is in `my_crate`'s test_utils, which implemented `MessageFull`
use my_crate::{PathInfo, ProtoLocal};
impl PathInfo for Mag { ... } // <- this violate rust's orphan rule
相反,我们可以:
// in doc test
// Included through `mod`.
mod test_utils;
use test_utils::msg::Msg;
use my_crate::{PathInfo, ProtoLocal};
impl PathInfo for Mag { ... }
// Here `Msg` is part of the doc test, just like `common` introduced in offical book about integration tests,
// and won't violate rust's orphan rule
# fn main() {} // <- This is essential, or an error will be raised
注意:您必须在文件末尾添加
# fn main() {}
,否则会出现错误。我现在不知道原因。
相关文档