Search code examples
c#rhino-mocks

Rhino Mocks: How to tell if object is mocked or real?


Given an object o, how can I tell if it's a mocked or a real object?

The only way I can see doing this looks a bit hacky:

public bool IsMockedObject(object o)
{
  try
  {
    o.GetMockRepository();
    return true;
  }
  catch(InvalidOperationException)
  {
    return false;
  }
}

Please tell me there's a better way!


Solution

  • You can check if the object implements IMockedObject:

    bool isMocked = o is Rhino.Mocks.Interfaces.IMockedObject;
    

    This of course would require referencing the RhinoMocks assembly, which I would try to avoid for your production code.