Closed Mrodent closed 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-dependency
s 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.
I am closing this but lmk if you are still having issues
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:If I put this:
... not surprisingly I get errors as follows:
... 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?