Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

rhino-mocks

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!

like image 391
DMac the Destroyer Avatar asked Sep 22 '11 21:09

DMac the Destroyer


1 Answers

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.

like image 95
BrokenGlass Avatar answered Sep 22 '22 16:09

BrokenGlass