asomers / mockall

A powerful mock object library for Rust
Apache License 2.0
1.5k stars 62 forks source link

"automock does not support this item type" #512

Closed peter-lyons-kehl closed 1 year ago

peter-lyons-kehl commented 1 year ago

Summary: custom attribute panicked from automock for a trivial (no fields, no trait) struct. Please help.

Using 1.74.0-nightly (2f5df8a94 2023-08-31). The same with the most recent (today's) 1.74.0-nightly (ec08a0337 2023-09-04), and with current stable v1.72.0 (after changing Cargo.toml not to use nightly feature).

Minimized source code: https://github.com/peter-kehl/mockall_enum_demo.

Cargo.toml = https://github.com/peter-kehl/mockall_enum_demo/blob/main/Cargo.toml:

[package]
name = "mockall_enum_demo"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

[dev-dependencies]
mockall_double = "0.3.0"
# Either of the following fails applying `mock` to struct `S` in mod `mockable` in src/lib.rs
# mockall = "0.11.4"
mockall = { version = "0.11.4", features = ["nightly"] }

src/lib.rs = https://github.com/peter-kehl/mockall_enum_demo/blob/main/src/lib.rs:

mod auto_mock;

#[cfg(test)]
mod tests {
    #[double]
    use crate::auto_mock::mockable::S;
    use mockall_double::double;
    //use crate::manu_mock::mockable::S;

    #[test]
    fn it_works() {
        fn with_s(s: S) {
            s.f();
        }
    }
}

src/manu_mock.rs = https://github.com/peter-kehl/mockall_enum_demo/blob/main/src/manu_mock.rs:

pub mod mockable {
    #[cfg(test)]
    use mockall::mock;

    pub struct S {}

    #[cfg(not(test))]
    impl S {
        pub fn f(self) {}
    }
    #[cfg(test)]
    mock! {
        pub S {
            pub fn f(self);
        }
    }
}
asomers commented 1 year ago

That's expected. When mocking a struct with no traits, you must put the #[automock] over the struct's impl block, not over the struct declaration. See https://docs.rs/mockall/latest/mockall/#mocking-structs .

peter-lyons-kehl commented 1 year ago

Indeed. Thank you.