Skip to content

Commit

Permalink
Created AsMockObject() extension method which MockRepository.GetMockO…
Browse files Browse the repository at this point in the history
…bject now delegates to - working to simplify surface area in MockRepository). Same process was done for MockRepository.GetMockedObjectOrNull()
  • Loading branch information
TimBarcz committed Sep 22, 2010
1 parent 61dc509 commit 79914b4
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 27 deletions.
27 changes: 2 additions & 25 deletions Rhino.Mocks/MockRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -855,11 +855,7 @@ private void IsMockObjectFromThisRepository(object obj)
/// </summary>
protected internal static IMockedObject GetMockedObject(object mockedInstance)
{
IMockedObject mockedObj = GetMockedObjectOrNull(mockedInstance);
if (mockedObj == null)
throw new InvalidOperationException("The object '" + mockedInstance +
"' is not a mocked object.");
return mockedObj;
return mockedInstance.AsMockObject();
}

/// <summary>
Expand All @@ -869,26 +865,7 @@ protected internal static IMockedObject GetMockedObject(object mockedInstance)
/// </summary>
protected internal static IMockedObject GetMockedObjectOrNull(object mockedInstance)
{
Delegate mockedDelegate = mockedInstance as Delegate;

if (mockedDelegate != null)
{
mockedInstance = mockedDelegate.Target;
}

// must be careful not to call any methods on mocked objects,
// or it may cause infinite recursion
if (mockedInstance is IMockedObject)
{
return (IMockedObject)mockedInstance;
}

if (RemotingMockGenerator.IsRemotingProxy(mockedInstance))
{
return RemotingMockGenerator.GetMockedObjectFromProxy(mockedInstance);
}

return null;
return mockedInstance.AsMockObjectOrNull();
}

/// <summary>Pops the recorder.</summary>
Expand Down
45 changes: 43 additions & 2 deletions Rhino.Mocks/RhinoMocksExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
using System;
using System.Collections.Generic;
using Rhino.Mocks.Exceptions;
using Rhino.Mocks.Generated;
using Rhino.Mocks.Generated;
using Rhino.Mocks.Impl.RemotingMock;
using Rhino.Mocks.Interfaces;


Expand Down Expand Up @@ -482,7 +483,47 @@ private VoidType()
}
}

#endregion
#endregion

internal static IMockedObject AsMockObject(this object mockedInstance)
{
IMockedObject mockedObj = mockedInstance.AsMockObjectOrNull();
if (mockedObj == null)
throw new InvalidOperationException("The object '" + mockedInstance +
"' is not a mocked object.");
return mockedObj;
}

/// <summary>
/// Method: GetMockedObjectOrNull
/// Get an IProxy from a mocked object instance, or null if the
/// object is not a mock object.
/// </summary>
internal static IMockedObject AsMockObjectOrNull(this object mockedInstance)
{
Delegate mockedDelegate = mockedInstance as Delegate;

if (mockedDelegate != null)
{
mockedInstance = mockedDelegate.Target;
}

// must be careful not to call any methods on mocked objects,
// or it may cause infinite recursion
if (mockedInstance is IMockedObject)
{
return (IMockedObject)mockedInstance;
}

if (RemotingMockGenerator.IsRemotingProxy(mockedInstance))
{
return RemotingMockGenerator.GetMockedObjectFromProxy(mockedInstance);
}

return null;
}


}
}
#endif

0 comments on commit 79914b4

Please sign in to comment.