nrxus / faux

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

Investigate how to use external mocks #27

Closed nrxus closed 2 years ago

nrxus commented 4 years ago

It would be great if users of a library could use faux to export mocked versions of their public API.

The problem is that items wrapped by #[cfg(test)] are intentionally not propagated through crates. Which means that any mocks created with the test flag are not lost when the crate is being used as a dependency.

Once we figure out a workaround for this we should document it (perhaps a mini guide?) on how to do it.

nrxus commented 4 years ago

My first idea was for something like:

# crate-a
[dependencies]
faux = { version = "0.4", optional = true }

[features]
exposed-mocks = ["faux"]
# crate-b
[dependencies]
crate-a = { version = "1.0" }

[dev-dependencies]
crate-a = { version = "1.0", features = exposed-mocks }

crate-b should now be able to use the mocks created by crate-a as long as the mocks are gated such as:

#[cfg(any(test,exposed-mocks))]
#[faux::create]
pub struct MyPublicStruct { /* fields go here */ }

#[cfg(any(test,exposed-mocks))]
#[faux::methods]
impl MyPublicStruct { /* methods go here */ }

Unfortunately this depends on: https://github.com/rust-lang/cargo/issues/7916

I might have to think of another way... :thinking:

nrxus commented 2 years ago

a solution to this is explained in: https://nrxus.github.io/faux/guide/exporting-mocks.html

cc: @kungfucop as I believe you were curious about this.