unit-testing - 测试自定义箱子

标签 unit-testing rust rust-crates

我在/src/lib.rs 中得到了这个 crate ,我正在尝试对其运行测试:

#![crate_type = "lib"]
#![crate_name = "mycrate"]

pub mod mycrate {
    pub struct Struct {
        field: i32,
    }

    impl Struct {
        pub fn new(n: i32) -> Struct {
            Struct { field: n }
        }
    }
}

/tests/test.rs 中的测试文件:

extern crate mycrate;

use mycrate::*;

#[test]
fn test() {
    ...
}

运行 cargo test 给出了这个错误:

tests/test.rs:3:5: 3:16 error: import `mycrate` conflicts with imported crate in this module (maybe you meant `use mycrate::*`?) [E0254]
tests/test.rs:3 use mycrate::*;
                     ^~~~~~~~~

我在这里做错了什么?

最佳答案

一个 crate 也自动成为一个有自己名字的模块。所以你不需要指定一个子模块。由于您导入了 mycrate crate 中的所有内容,因此您还导入了 mycrate::mycrate 模块,这导致了命名冲突。

只需将 src/lib.rs 的内容更改为

pub struct Struct {
    field: i32,
}

impl Struct {
    pub fn new(n: i32) -> Struct {
        Struct { field: n }
    }
}

也不需要 crate_namecrate_type 属性。

关于unit-testing - 测试自定义箱子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34723575/

相关文章:

Java 单元测试 - 常量字段与 setUp()

javascript - 我如何创建 JavaScript 测试用例

generics - 如何在 Rust 中转换泛型原始类型?

rust - 当依赖项的创建成本很高时该怎么办?

rust - 子模块如何从 lib.rs 的根目录导入特征?

unit-testing - 应该在单元测试之前还是之后编写 stub ?

c# - 使用起订量,System.InvalidCastException : Unable to cast object of type 'CaSTLe.Proxies.ObjectProxy' to type

rust - MeiliSearch with Rust SDK搜索查询有限制

rust - 如何查询主机三元组的 rustc?

rust - 生成脚本无法找到[dependencies]部分中列出的 crate