DEAR PEOPLE FROM THE FUTURE: Here's what we've figured out so far...

Welcome! This is a Q&A website for computer programmers and users alike, focused on helping fellow programmers and users. Read more

What are you stuck on? Ask a question and hopefully somebody will be able to help you out!
+2 votes

I just wanted to separate this example in different files
project/src/main.rs

#[cfg(test)] mod tests;

#[macro_use] extern crate rocket;

#[get("/")]
fn index() -> &'static str {
    "Hello, world!"
}

#[launch]
fn rocket() -> _ {
    rocket::build().mount("/", routes![index])
}

and project/tests/main.rs

#[macro_use] extern crate rocket;

#[cfg(test)]
mod test {
    use super::rocket;
    use rocket::local::Client;
    use rocket::http::Status;

    #[test]
    fn hello_world() {
        let client = Client::new(rocket()).expect("valid rocket instance");
        let mut response = client.get("/").dispatch();
        assert_eq!(response.status(), Status::Ok);
        assert_eq!(response.body_string(), Some("Hello, world!".into()));
    }
}

But I'm getting the following error

❯ cargo test
   Compiling jedi_archives v0.0.0 (/home/user/sync/code/rust/jedi_archives)
error[E0583]: file not found for module `tests`
 --> src/main.rs:1:14
  |
1 | #[cfg(test)] mod tests;
  |              ^^^^^^^^^^
  |
  = help: to create the module `tests`, create file "src/tests.rs" or "src/tests/mod.rs"

error[E0432]: unresolved import `rocket::local::Client`
 --> tests/main.rs:6:9
  |
6 |     use rocket::local::Client;
  |         ^^^^^^^^^^^^^^^------
  |         |              |
  |         |              help: a similar name exists in the module (notice the capitalization): `client`
  |         no `Client` in `local`

error[E0423]: expected function, found crate `rocket`
  --> tests/main.rs:11:34
   |
11 |         let client = Client::new(rocket()).expect("valid rocket instance");
   |                                  ^^^^^^ not a function

warning: unused `#[macro_use]` import
 --> tests/main.rs:1:1
  |
1 | #[macro_use] extern crate rocket;
  | ^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

Some errors have detailed explanations: E0423, E0432.
For more information about an error, try `rustc --explain E0423`.
warning: `jedi_archives` (test "main") generated 1 warning
error: could not compile `jedi_archives` due to 2 previous errors; 1 warning emitted
warning: build failed, waiting for other jobs to finish...
For more information about this error, try `rustc --explain E0583`.
error: build failed
by
0

For a starter, you have created the file project/tests/main.rs but you're expected to use project/src/tests.rs or project/src/tests/mod.rs instead. The tests folder that you've created in the root directory alongside src is meant for integration tests according to the documentation, and it only works with libraries (no binaries).

2 Answers

+2 votes
 
Best answer

project/src/main.rs

#![feature(proc_macro_hygiene, decl_macro)]

#[macro_use]
extern crate rocket;

#[cfg(test)]
mod tests;

#[get("/")]
fn hello() -> &'static str {
    "Hello, world!"
}

fn rocket() -> rocket::Rocket {
    rocket::ignite().mount("/", routes![hello])
}

fn main() {
    rocket().launch();
}

project/src/tests.rs

use super::rocket;
use rocket::local::Client;
use rocket::http::Status;

#[test]
fn test_hello() {
    let client = Client::new(rocket()).unwrap();
    let mut response = client.get("/").dispatch();
    assert_eq!(response.status(), Status::Ok);
    assert_eq!(response.body_string(), Some("Hello, world!".into()));
}
by
selected by
+1 vote

For rocket v0.5-rc:

project/src/main.rs

#[macro_use] extern crate rocket;

#[cfg(test)] mod main_test;

#[get("/")]
fn index() -> &'static str {
    "Hello, world!"
}

#[launch]
fn rocket() -> _ {
    rocket::build().mount("/", routes![index])
}

project/src/main_test.rs

use super::rocket;
use rocket::http::Status;

#[test]
fn test_hello() {
    use rocket::local::blocking::Client;

    let client = Client::tracked(rocket()).unwrap();
    let response = client.get("/").dispatch();
    assert_eq!(response.status(), Status::Ok);
    assert_eq!(response.into_string(), Some("Hello, world!".into()));
}
by
edited by
Contributions licensed under CC0
...