Open kungfucop opened 4 years ago
It should be possible. If the dependency is built with mocking enabled for the function, it will be available in the depdendee. You probably should add a special feature gate to enable that, the regular #[cfg(test)]
items are not enabled when built as a dependency.
I just ran into the same issue with a project I am working on and found this helpful: https://github.com/rust-lang/rust/issues/45599
I changed my Cargo.toml file to make mocktopus an optional dependency under dependencies
instead of dev-dependencies
and added a "mockable" feature:
[dependencies]
mocktopus = { version = "*", optional = true }
[features]
mockable = ["mocktopus"]
In the dependency library I wished to mock I changed
#[cfg(test)]
use mocktopus::macros::*;
to
#[cfg(any(feature = "mockable", mockable))]
use mocktopus::macros::*;
and
#[cfg_attr(test, mockable)]
pub fn the_function_i_want_to_mock() {
...
}
to
#[cfg_attr(feature = "mockable", mockable)]
pub fn the_function_i_want_to_mock() {
...
}
and I enable the feature only when running tests with cargo +nightly test --features "mockable"
Thank you @ekump for the share, and I worked around in same way!
I have 2 creates in my project, base and service, and service crate based on base. And the code is something like this:
I expect db.get_users() is not called, but mock result returns, but it actually didn't respect the mock and goes to expect the real db.get_users() code. Does mocktopus support mocking on method in different crate or I made mistakes?
Thanks a lot