asomers / mockall

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

Cannot mock a trait that returns `Option<Self> where Self: Sized` in async test function #433

Closed Pet3ris closed 1 year ago

Pet3ris commented 1 year ago

Don't seem to be able to mock the following trait:

trait Zen {
    fn zen() -> Option<Self> where Self: Sized;
}
mock! {
    pub Z {}
    impl Zen for Z {
        fn zen() -> Option<Self> where Self: Sized;
    }
}

In mockall = "0.11.2".

Detailed error

error[E0282]: type annotations needed
   --> src/commands/run.rs:311:9
    |
311 | /         mock! {
312 | |             pub Z {}
313 | |             impl Zen for Z {
314 | |                 fn zen() -> Option<Self> where Self: Sized;
315 | |             }
316 | |         }
    | |_________^ cannot infer type
asomers commented 1 year ago

Your exact code works for me. Please post a complete example.

Pet3ris commented 1 year ago

Could this have something to do with tokio-test?

#[cfg(test)]
mod tests {
    use async_trait::async_trait;
    use mockall::*;

    use super::*;

    #[tokio::test]
    async fn test_mockall() {
        trait Zen {
            fn zen() -> Option<Self> where Self: Sized;
        }
        mock! {
            pub Z {}
            impl Zen for Z {
                fn zen() -> Option<Self> where Self: Sized;
            }
        }
    }
}
Pet3ris commented 1 year ago

This works:

#[cfg(test)]
mod tests {
    use async_trait::async_trait;
    use mockall::*;

    use super::*;

    trait Zen {
        fn zen() -> Option<Self> where Self: Sized;
    }
    mock! {
        pub Z {}
        impl Zen for Z {
            fn zen() -> Option<Self> where Self: Sized;
        }
    }

    #[tokio::test]
    async fn test_mockall() {
        let mock = MockZ::new();
    }
}