How to mock traits' methods with not clonable arguments and return, using double crate?


#1

Hey!

I’m trying to use double crate to mock traits. So far so good, except when the trait in question has not clonable arguments or return types, like Read.

The read method asks for a &mut [u8] as argument and a io::Result<usize> (equivalent to Result<usize, io::Error>) as return. Both &mut [u8] and io::Error don’t implement Clone.

So, the double’s Mock is defined as:

pub struct Mock<C, R> where
    C: Clone + Eq + Hash,
    R: Clone,  { /* fields omitted */ }

And the documentation states:

Used for tracking function call arguments and specifying a predetermined return value or mock function.
See the crate documentation for more substantial examples, including some that demonstrate how to use Mock for methods that have multiple arguments as well as methods with argument or return types that do not implement Clone.

But I was unable to find anything mentioning how to mock not clonable things in the documentation.

How can I mock trait like Read with double? There is a way to encapsulate not clonable types in a clonable container?


#2

Well, my bad but there is an example in the repository showing how to handle io::Result. Here.

And I managed to work with &mut [u8] arg converting it to Vec<u8>.

My code can be found here.