Mocking a Send + Sync type?

I use mock objects extensively in my unit testing. But now I need to mock a type that is Send and Sync, and I can’t do it. None of the mock libraries I checked (galvanic_mock, mock-it, pseudo, double, mock_derive, mockers, and simulacrum) can do it. I’ve tried mucking around with the Fragile crate. With difficulty, I think I can get it working for Send types, but not for Sync ones. Has anybody run into this problem before?

1 Like

Can you give a code example of something that’s not working? That might make it easier for people to help point you in the right direction.

Here’s a minimal example with Mockers. It doesn’t work of course, which is why it’s commented out. The basic idea is "create a mock for trait T, then turn it into a trait object for T + Send.

But I came up with another “solution” after getting no response for 24h. My “solution” is incredibly lame. I use Simulacrum to create a mock structure, then cheat by implementing Send! It’s totally unsafe, but it works as long as my unit tests are all single-threaded. It looks a bit like this:

use simulacrum::Expectations;

pub struct MyMock {
    e: Expectations
impl MyMock {
unsafe impl Send for MyMock {}

I can get away with this because my unit tests (which use mocks) are single-threaded, even though my functional tests (which don’t) aren’t.

1 Like