nrxus / faux

Struct mocking library for Rust
https://nrxus.github.io/faux/
MIT License
411 stars 14 forks source link

Don't understand dependency instructions #60

Closed Mrodent closed 2 months ago

Mrodent commented 2 months ago

Thanks for developing this ... as a pretty new Rust user I'm finding many aspects of Rust testing very challenging.

So this crate appears to offer the possibility of stubbing, without "polluting" the app code.

The example given works on my machine (having replaced the main with directives #[cfg(test)] and #[test]. But... only if I put this in my Cargo.toml:

[dependencies]
faux = "0.1.10"

If I put this:

[dev-dependencies]
faux = "0.1.10"

... not surprisingly I get errors as follows:

error[E0433]: failed to resolve: use of undeclared crate or module `faux`
  --> src\lib.rs:21:7
   |
21 |     #[faux::create]
   |       ^^^^ use of undeclared crate or module `faux`

error[E0433]: failed to resolve: use of undeclared crate or module `faux`
  --> src\lib.rs:30:7
   |
30 |     #[faux::methods]
   |       ^^^^ use of undeclared crate or module `faux`

... i.e. inside the mod client block in your example.

... and yet no-one else has raised this question. So I'm left feeling a bit naive... could you explain what I should do here?

nrxus commented 2 months ago

Hey @Mrodent, in the Getting Started section of the README, I called out that you need to wrap the attributes around #[cfg_attr(test, ..)].

Such as:

#[cfg_attr(test, faux::create)]
pub struct MyStructToMock { /* fields */ }

#[cfg_attr(test, faux::methods)]
impl MyStructToMock { /* methods to mock */ }

What this does is that at compile time Rust will check "am I compiling for test or not?" If compiling for tests then the faux macro is activated, and since in tests the dev-dependency will be built then it will work. When not compiling for test, Rust will ignore the macro and thus faux won't touch your production code at all.

The reason you are getting an error is because dev-dependencys are not included during a non-test build, thus faux won't be included in your build and then it fails to find it. I hope that makes sense.

nrxus commented 2 months ago

I am closing this but lmk if you are still having issues