#[automock] added only in test config but mock structure not recognized in integration test

473 Views Asked by At

I've been playing with mockall for a few days, it's working well with the crate added in the dependencies but now I'd like to move it to my dev-dependencies.

I've seen on the GitHub Readme that we can do something like this

#[cfg(test)]
use mockall::{automock, mock, predicate::*};
#[cfg_attr(test, automock)]
trait MyTrait {
    fn foo(&self, x: u32) -> u32;
}

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

    #[test]
    fn mytest() {
        let mut mock = MockMyTrait::new();
        mock.expect_foo()
            .with(eq(4))
            .times(1)
            .returning(|x| x + 1);
        assert_eq!(5, mock.foo(4));
    }
}

It's working well with a unit test as per the example but not in a separate integration test. I have an integration test inside a tests directory in which I'm trying to import my MockMyTrait struct created by the #[automock] attribute macro.

#[cfg(test)]
use automock_test::foo::MockMyTrait;

I get the autocomplete working in VS Code with rust-analyzer, it's actually detecting that my MockMyTrait exists but then I get an error telling me this is an unresolved import.

So is there a way to add mockall as a dev-dependencies only, use automock with the test config and reference our mock structure from an integration test?

I'd like to keep my application clean of all test-only dependencies.

0

There are 0 best solutions below