dimanche 4 avril 2021

Types defined in src/main.rs are not recognized in tests/test.rs file

I tried to write a unit test in rust, but when I run cargo test I get the following error: "use of undeclared type Rating". In the src/main.rs file I have defined the struct Rating like this:

#[derive(PartialEq, Debug, Clone, Copy)]
struct Rating(i8);

impl Rating {
    pub fn new(value: i32) -> Result <Rating, CreationError> {
        match value {
            v if v > 10 => Err(CreationError::PosOverflow),
            v if v < -10 => Err(CreationError::NegOverflow),
            _ => Ok(Rating(value as i8)),
        }

    }
}

My test file tests/test.rs looks like this:

#[cfg(test)]
fn create_new_rating() {
    assert_eq!(Rating::new(10).0, 10);
}

In the Rust documentation I only found examples where libs are tested but not binarys. Do I have to use a different syntax in this case?

Aucun commentaire:

Enregistrer un commentaire