From 79914b4a6f70c3eb1caec46c0834b5fdead65ceb Mon Sep 17 00:00:00 2001 From: TimBarcz Date: Wed, 22 Sep 2010 07:43:57 -0700 Subject: [PATCH] Created AsMockObject() extension method which MockRepository.GetMockObject now delegates to - working to simplify surface area in MockRepository). Same process was done for MockRepository.GetMockedObjectOrNull() --- Rhino.Mocks/MockRepository.cs | 27 ++--------------- Rhino.Mocks/RhinoMocksExtensions.cs | 45 +++++++++++++++++++++++++++-- 2 files changed, 45 insertions(+), 27 deletions(-) diff --git a/Rhino.Mocks/MockRepository.cs b/Rhino.Mocks/MockRepository.cs index 53a16e40..53553c0d 100644 --- a/Rhino.Mocks/MockRepository.cs +++ b/Rhino.Mocks/MockRepository.cs @@ -855,11 +855,7 @@ private void IsMockObjectFromThisRepository(object obj) /// 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(); } /// @@ -869,26 +865,7 @@ protected internal static IMockedObject GetMockedObject(object mockedInstance) /// 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(); } /// Pops the recorder. diff --git a/Rhino.Mocks/RhinoMocksExtensions.cs b/Rhino.Mocks/RhinoMocksExtensions.cs index 5e796b6c..7f546959 100644 --- a/Rhino.Mocks/RhinoMocksExtensions.cs +++ b/Rhino.Mocks/RhinoMocksExtensions.cs @@ -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; @@ -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; + } + + /// + /// Method: GetMockedObjectOrNull + /// Get an IProxy from a mocked object instance, or null if the + /// object is not a mock object. + /// + 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