无法在集成测试中导入模块 [英] Cannot import a module in an integration test

查看:64
本文介绍了无法在集成测试中导入模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Rust 中配置一个示例项目以使其工作.

I am trying to configure an example project in Rust to work.

我的结构是:

  • src/potter.rs
  • tests/tests.rs

还有我的Cargo.toml

[package]
name = "potter"
version = "0.1.0"
authors = ["my name"]
[dependencies]

我的 potter.rs 包含:

pub mod potter {
    pub struct Potter {

    }

    impl Potter  {
        pub fn new() -> Potter {
         return Potter {};
        }
    }

}

我的 tests.rs 包含:

use potter::Potter;

    #[test]
    fn it_works() {

        let pot = potter::Potter::new();
        assert_eq!(2 + 2, 4);
    }

但我收到此错误:

error[E0432]: unresolved import `potter`
 --> tests/tests.rs:1:5
  |
1 | use potter::Potter;
  |     ^^^^^^ Maybe a missing `extern crate potter;`?

error[E0433]: failed to resolve. Use of undeclared type or module `potter`
 --> tests/tests.rs:6:19
  |
6 |         let pot = potter::Potter::new();
  |                   ^^^^^^ Use of undeclared type or module `potter`

warning: unused import: `potter::Potter`
 --> tests/tests.rs:1:5
  |
1 | use potter::Potter;
  |     ^^^^^^^^^^^^^^
  |
  = note: #[warn(unused_imports)] on by default

如果我添加 extern crate potter;,它不会解决任何问题...

If I add extern crate potter;, it doesn't fix anything...

error[E0463]: can't find crate for `potter`
 --> tests/tests.rs:1:1
  |
1 | extern crate potter;
  | ^^^^^^^^^^^^^^^^^^^^ can't find crate

推荐答案

返回并重读Rust 编程语言关于包、包、模块和文件系统.

Go back and reread The Rust Programming Language about packages, crates, modules and the filesystem.

常见痛点:

  • 每种编程语言都有自己处理文件的方式——你不能仅仅假设因为你使用过任何其他语言,你会神奇地得到 Rust 的理解.这就是为什么你应该 回去重新- 阅读有关它的书籍章节.

每个文件定义一个模块.你的 lib.rs 定义了一个与你的 crate 同名的模块;mod.rs 定义一个与其所在目录同名的模块;每隔一个文件定义一个文件名的模块.

Each file defines a module. Your lib.rs defines a module of the same name as your crate; a mod.rs defines a module of the same name as the directory it's in; every other file defines a module of the name of the file.

你的库包的根目录必须lib.rs;二进制 crate 可以使用 main.rs.

The root of your library crate must be lib.rs; binary crates may use main.rs.

不,您真的不应该尝试进行非惯用的文件系统组织.有一些技巧可以做你想做的任何事情;除非您已经是高级 Rust 用户,否则这些想法很糟糕.

No, you really shouldn't try to do non-idiomatic filesystem organization. There are tricks to do most anything you want; these are terrible ideas unless you are already an advanced Rust user.

惯用的 Rust 通常不会像许多其他语言一样放置每个文件一种类型".对真的.您可以在一个文件中包含多个内容.

Idiomatic Rust does not generally place "one type per file" like many other languages. Yes, really. You can have multiple things in one file.

单元测试通常与它正在测试的代码位于同一个文件中.有时它们会被拆分成一个包含子模块的文件,但这种情况并不常见.

Unit tests usually live in the same file as the code it's testing. Sometimes they will be split out into a file containing the submodule, but that's uncommon.

集成测试、示例、基准测试都必须像 crate 的任何其他用户一样导入 crate,并且只能使用公共 API.

Integration tests, examples, benchmarks all have to import the crate like any other user of the crate and can only use the public API.

要解决您的问题:

  1. 将您的 src/potter.rs 移动到 src/lib.rs.
  2. src/lib.rs 中删除 pub mod potter.不是严格必需的,但删除了不必要的模块嵌套.
  3. extern crate potter 添加到您的集成测试 tests/tests.rs.
  1. Move your src/potter.rs to src/lib.rs.
  2. Remove pub mod potter from src/lib.rs. Not strictly required, but removes needless nesting of modules.
  3. Add extern crate potter to your integration test tests/tests.rs.

文件系统

├── Cargo.lock
├── Cargo.toml
├── src
│   └── lib.rs
├── target
└── tests
    └── tests.rs

src/lib.rs

pub struct Potter {}

impl Potter {
    pub fn new() -> Potter {
       Potter {}
    }
}

tests/tests.rs

use potter::Potter;

#[test]
fn it_works() {
    let pot = Potter::new();
    assert_eq!(2 + 2, 4);
}

这篇关于无法在集成测试中导入模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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