How can I use fields of a mocked struct using mockall?

⚓ Rust    📅 2025-07-18    👤 surdeus    👁️ 4      

surdeus

I'm hoping to make a stateful mock object by using mockall to mock a struct with fields I control similar to the following:

struct Counter {
    pub count: u8,
}

mock! {
    pub Counter {}
}

let test = MockCounter::new();
dbg!("Count is {:?}", test.count)

but this fails to compile, complaining that there is no field count on type MockCounter. Is there any way to access custom fields on a mock object or otherwise track custom state? Only other option that comes to mind is static variables, but I would prefer to stay in the scope of mock object definitions for cleanliness and simplicity if possible.

2 posts - 2 participants

Read full topic

🏷️ rust_feed