Is there way to make cloneable expectations? #327
Replies: 2 comments 4 replies
-
Are you asking if the Mock object can automatically implement |
Beta Was this translation helpful? Give feedback.
-
Clone being part of the mocked interface never sat right with me. @asomers is totally right that it is never sensible to implement // Another trait stipulates that implementing struct must be Clone
trait EventHandler : Clone {
fn handle(&self) {}
}
// Service<T> here is Clone, because T is Arc<Dependency>, which is clone
impl<T> EventHandler for Service<T>
where
T: Deref<Target: Dependency> + Clone,
{
fn handle(&self) {
self.t.do_thing();
}
} Note: this requires newer rust (works on 1.81). On older you'd have to do I'm saying here that a dependency isn't of trait Mocking semantics are also maintained: If your service starts up a bunch of threads and has to share a single instance of your dependency across them somehow, it'll do the same with your mock, exactly as it would do in production code For dependencies with mutable receiver I think it should be possible to use |
Beta Was this translation helpful? Give feedback.
-
I try to mock DB repo. (In app logic repo is a cloneable object.)
I use
mock!
macro to achieve Clone behavior for MockRepo. In order to expectation of repo functions for cloned object also works, I use this construction:Is there more direct way to achieve this?
MockRepo definition
upd: When it's needed to multiple clone, this approach is used by me:
Beta Was this translation helpful? Give feedback.
All reactions