-
We are introducing mocks into our codebase, and I'm running into a problem. We are testing a function that takes in a struct as an argument, but we want to mock that struct. If we use the instructions (mockall_double, etc...), the compiler type-errors (Expected Foo, got MockFoo). The solution for now, is to mock fn uses_foo<F: Foo>(some_arg: (), foo: F) {
....
} ...this is okay-ish, but as we start using mockall throughout our codebase, we will start getting things like struct LotsOfComponents<F: FooComponentTrait, B: BarComponentTrait, A: AlphaWorkerTrait, B: BravoWorkerTrait, ....> {
foo: F,
foo_worker: FW,
some_name: A,
another_name: B,
running_out_of_names: C,
how_many_generics_do_we_need: HMGDWN,
...
} For reference: massalabs/massa#3745 (comment) |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 26 replies
-
I've created an issue that hypothesizes a workable approach> #479 |
Beta Was this translation helpful? Give feedback.
-
You must be doing something wrong. What you describe is exactly what |
Beta Was this translation helpful? Give feedback.
-
I'm revisiting our codebase. usage: let mut mocked1 = NetworkCommandSender::default();
let mut mocked2 = NetworkCommandSender::default();
mocked2
.expect_get_bootstrap_peers()
.times(1)
.returning(|| Ok(get_peers()));
mocked1.expect_clone().return_once(move || mocked2); imported with: #[mockall_double::double]
use massa_network_exports::network_controller::NetworkCommandSender; mocked in the #[cfg_attr(any(test, feature = "testing"), mockall::automock)]
impl NetworkCommandSender { I tried re-exporting with mock_double in the structs parent module, then just importing naivly everywhere else (no use of mockdouble except at the re-export) and that seems to work :) |
Beta Was this translation helpful? Give feedback.
-
This is a problem . #[cfg(any(test, feature = "testing"))]
use massa_network_exports::network_controller::MockNetworkCommandSender as NetworkCommandSender;
#[cfg(not(any(test, feature = "testing")))]
use massa_network_exports::network_controller::NetworkCommandSender;
This means that you tried to call the |
Beta Was this translation helpful? Give feedback.
-
By any chance, is |
Beta Was this translation helpful? Give feedback.
Then that's your problem. Because when you build a crate in
test
mode, its dependencies are not build intest
mode. So what you want to do is probably to create atesting
feature for themassa_network_exports
crate and export mock objects when that feature is set, not just whentest
is set. Then in your consuming create, ensure that you use the dependency'stesting
feature whenever you build intest
mode. Or, combine the two crates into one.