mockall and zbus, problem of lifetime #492
Replies: 3 comments 12 replies
-
This is a limitation of Mockall. It can't currently mock constructor methods for structs with a lifetime parameter. It might be possible to relax this limitation by always returning mock! {
pub HelloProxy<'static> {
pub async fn new(conn: &MockConnection) -> zbus::Result<Self<'static>>;
}
} |
Beta Was this translation helpful? Give feedback.
-
Hello @asomers have you made any progress about the suggestion you made on return a static lifetime mock ? Thank you for your work on mockall ! |
Beta Was this translation helpful? Give feedback.
-
Honestly, everything in mockall_derive is difficult for a rookie. Writing proc macros is harder than writing normal Rust code. So my advice would be that you don't wait for me. Instead, work around this limitation by yourself. You can do it by manually implementing the pub mod mock {
use mockall::*;
mock! {
pub Connection {
pub async fn system() -> zbus::Result<Self>;
}
}
mock! {
pub HelloProxy {
pub async fn hello(&self, msg: &str) -> zbus::Result<()>;
}
}
impl MockHelloProxy {
pub async fn new(_conn: &MockConnection) -> zbus::Result<Self> {
Ok(MockConnection::default)
)
}
} |
Beta Was this translation helpful? Give feedback.
-
Hello there,
I'm starting to learn rust and I have a use case where I need to make dbus call, for this , I use zbus. And for unit test , I chose to use mockall (awesome crate by the way).
For example , let's say that I have the following Trait (which abstract dbus proxy):
I cannot use automock because of the proc macro dbus_proxy (if you know how, please tell me), the compiler throw me this error when I put
#[mockall::automock]
above dbus proxy:Nevertheless, I manage to have something working by the use of
mock
macro like that:And my unit test work well (providing limitations that are stated in https://docs.rs/mockall/latest/mockall/#static-methods) and I can mock the various dbus methods to return error or tested value by using different import in test :
But my application is going more complex and I need to put the proxy type inside a struct and this require adding a lifetime parameter (I choose static for simplicity):
But doing that says my mock doesn't have a lifetime parameter and when I didn't manage to add one in mock definition:
Doing that raises:
And using another lifetime name like <'a> throw me:
I looked at #293 and #348 but it didn't help me to understand how can I achieve what I want.
Any advices on how I can mange to mock the zbus proxy when it requires a lifetime parameter ?
Beta Was this translation helpful? Give feedback.
All reactions