asomers / mockall

A powerful mock object library for Rust
Apache License 2.0
1.45k stars 61 forks source link

Mock Trait that is a composition of two other traits #549

Closed Tarcontar closed 7 months ago

Tarcontar commented 7 months ago

Hi, I want to mock a trait that is composed of two other traits like so:

#[cfg(test)]
use mockall::{automock, predicate::*};

#[cfg_attr(test, automock)]
pub trait A {

}

#[cfg_attr(test, automock)]
pub trait B {

}

#[cfg_attr(test, automock)]
pub trait AB: A + B {

}

Result in the error: "the trait bound '..MockAB: B' is not satisfied the trait B is implemented for MockB"

How would I do that? Thanks!

Tarcontar commented 7 months ago

Found it in the examples:

pub trait A {
    fn foo(&self);
}

pub trait B: A {
    fn bar(&self);
}

mock! {
    // Structure to mock
    C {}
    // First trait to implement on C
    impl A for C {
        fn foo(&self);
    }
    // Second trait to implement on C
    impl B for C {
        fn bar(&self);
    }
}

But would be nice if that was also supported by automock!

asomers commented 7 months ago

In fact, I have a plan for how to do just that! I have a partial implementation too, but it isn't ready yet. Still a few corner cases to take care of. See https://github.com/asomers/mockall/discussions/539 .