asomers / mockall

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

mocking struct & mockall_double::double not working as expected #564

Closed zaidoon1 closed 5 months ago

zaidoon1 commented 5 months ago

project directory:

src
----> main.rs
----> storage
          --------> client.rs

client.rs:

#[cfg(test)]
use mockall::automock;

pub struct HelloClient {
    client: ....,
}

#[cfg_attr(test, automock)]
impl HelloClient {
......
} 

main.rs:

#[mockall_double::double]
use <project_name>::storage::client::HelloClient;

async fn delete_records(
    records: &[Vec<u8>],
    hello_client: &HelloClient,
) {
xxxx
}

compiler complains with:

unresolved import `<project_name>::storage::client::MockHelloClient`
no `MockHelloClient` in `storage::client`

If I move mockall from dev-dependencies to dependencies in cargo.toml and and update storage.rs to:

#[automock]
impl HelloClient {
......
} 

Things appear to work? But I don't want mockall to be a dependency. What am I doing wrong?

asomers commented 5 months ago

What you posted looks correct. Perhaps the problem lies somewhere else? Are there any other imports?

zaidoon1 commented 5 months ago

maybe it's in the mod file?

What I have is:

src
----> main.rs
----> storage
          --------> client.rs
          ---------> mod.rs

my mod.rs looks like:

 pub mod client;

I already posted the entire code for client.rs. I have a lib.rs in the same directory as main.rs that has:

 pub mod storage;

That's pretty much it..

asomers commented 5 months ago

When do you see the error? Is it when you do cargo build or cargo test --bin ? What version of Mockall are you using?

zaidoon1 commented 5 months ago

I'm using the latest version 0.12.1. I see it in cargo build, cargo test and vscode linter when i'm looking at the code.

asomers commented 5 months ago

But cargo test does lots of things: unit tests, integration tests, and doc tests. It even checks the examples. That's why I asked specifically for cargo test --bin. That will tell you if your project builds successfully in test mode.

zaidoon1 commented 5 months ago

just ran it with cargo test --bin:


error[E0432]: unresolved import `<project_name>::storage::client::MockHelloClient`
  --> src/main.rs:38:5
   |
38 | use < project_name>::storage::client:: HelloClient;
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-------------
   |     |                                     |
   |     |                                     help: a similar name exists in the module: `HelloClient`
   |     no `MockHelloClient ` in `storage::client`

For more information about this error, try `rustc --explain E0432`.
error: could not compile `project_name` (bin "project_name" test) due to 1 previous error
asomers commented 5 months ago

That tells you that your project fails to build in test mode. You also said that it fails during cargo build. But that error message doesn't make sense for non-test mode. Are you sure that you get the same error during cargo build?

zaidoon1 commented 5 months ago

sorry, that's my bad, cargo build works fine.

asomers commented 5 months ago

Your problem is that your crate contains both a library and a binary. When you build the binary in test mode, the library is built in non-test mode, just like an external dependency. So you need to either:

zaidoon1 commented 5 months ago

oh i see. thank you!