如何在测试范围的mod中使用注解和微观?
How do I use the annotation and micro in the mod of the test scope?
我的代码结构如下。
我 运行 'cargo run' 并且有效。但是当我 运行 'cargo test' 时,我得到如下错误。你能告诉我为什么以及如何解决它们吗?
error: cannot find attribute get
in this scope
error: cannot find macro routes
in this scope
src/main.rs
#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use]
extern crate rocket;
mod common;
fn main() {
common::run();
}
src/common.rs
#[get("/hello")]
pub fn hello() -> &'static str {
"hello"
}
pub fn run() {
rocket::ignite().mount("/", routes![hello]).launch();
}
tests/development.rs
#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use]
extern crate rocket;
#[cfg(test)]
mod common;
#[test]
fn test_development_config() {
common::run();
}
tests/common.rs
use rocket::http::Status;
use rocket::local::Client;
#[get("/check_config")]
fn check_config() -> &'static str {
"hello"
}
pub fn run() {
let rocket = rocket::ignite().mount("/", routes![check_config]);
let client = Client::new(rocket).unwrap();
let response = client.get("/hello").dispatch();
assert_eq!(response.status(), Status::Ok);
}
tests/
文件夹中的每个.rs
文件作为测试单独编译执行。所以 development.rs
被编译, "includes" common.rs
并且它起作用了。但是随后 common.rs
被单独编译并且失败,因为任何地方都没有 #[macro_use] extern crate rocket;
。
一个解决方案是将您的 common.rs
放入 tests/common/mod.rs
。 tests
的子目录中的文件不会自动编译为测试。
我的代码结构如下。
我 运行 'cargo run' 并且有效。但是当我 运行 'cargo test' 时,我得到如下错误。你能告诉我为什么以及如何解决它们吗?
error: cannot find attribute
get
in this scopeerror: cannot find macro
routes
in this scope
src/main.rs
#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use]
extern crate rocket;
mod common;
fn main() {
common::run();
}
src/common.rs
#[get("/hello")]
pub fn hello() -> &'static str {
"hello"
}
pub fn run() {
rocket::ignite().mount("/", routes![hello]).launch();
}
tests/development.rs
#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use]
extern crate rocket;
#[cfg(test)]
mod common;
#[test]
fn test_development_config() {
common::run();
}
tests/common.rs
use rocket::http::Status;
use rocket::local::Client;
#[get("/check_config")]
fn check_config() -> &'static str {
"hello"
}
pub fn run() {
let rocket = rocket::ignite().mount("/", routes![check_config]);
let client = Client::new(rocket).unwrap();
let response = client.get("/hello").dispatch();
assert_eq!(response.status(), Status::Ok);
}
tests/
文件夹中的每个.rs
文件作为测试单独编译执行。所以 development.rs
被编译, "includes" common.rs
并且它起作用了。但是随后 common.rs
被单独编译并且失败,因为任何地方都没有 #[macro_use] extern crate rocket;
。
一个解决方案是将您的 common.rs
放入 tests/common/mod.rs
。 tests
的子目录中的文件不会自动编译为测试。