From: <fli...@us...> - 2008-12-19 02:41:31
|
Revision: 199 http://structuremap.svn.sourceforge.net/structuremap/?rev=199&view=rev Author: flimflan Date: 2008-12-19 02:41:25 +0000 (Fri, 19 Dec 2008) Log Message: ----------- Removed static reference to Rhino.Mocks.dll in AutoMocker - Allows Ayende to release as often as he wants, and we dont have to stay in synch - Allows MVBA to continue to live on the bleeding edge Modified Paths: -------------- trunk/Source/StructureMap.AutoMocking/RhinoAutoMocker.cs trunk/Source/StructureMap.AutoMocking/ServiceLocator.cs trunk/Source/StructureMap.AutoMocking/StructureMap.AutoMocking.csproj trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj Added Paths: ----------- trunk/Source/StructureMap.AutoMocking/RhinoMockRepositoryProxy.cs trunk/Source/StructureMap.Testing/AutoMocking/RhinoMockRepositoryProxyTester.cs Modified: trunk/Source/StructureMap.AutoMocking/RhinoAutoMocker.cs =================================================================== --- trunk/Source/StructureMap.AutoMocking/RhinoAutoMocker.cs 2008-12-17 16:41:58 UTC (rev 198) +++ trunk/Source/StructureMap.AutoMocking/RhinoAutoMocker.cs 2008-12-19 02:41:25 UTC (rev 199) @@ -1,9 +1,4 @@ using System; -using System.Collections; -using System.Collections.Generic; -using System.Reflection; -using Rhino.Mocks; -using StructureMap.Graph; namespace StructureMap.AutoMocking { Added: trunk/Source/StructureMap.AutoMocking/RhinoMockRepositoryProxy.cs =================================================================== --- trunk/Source/StructureMap.AutoMocking/RhinoMockRepositoryProxy.cs (rev 0) +++ trunk/Source/StructureMap.AutoMocking/RhinoMockRepositoryProxy.cs 2008-12-19 02:41:25 UTC (rev 199) @@ -0,0 +1,53 @@ +using System; +using System.Reflection; + +namespace StructureMap.AutoMocking +{ + public class RhinoMockRepositoryProxy + { + private readonly object _instance; + private readonly Func<Type, object[], object> _PartialMock; + private readonly Action<object> _Replay; + private readonly Func<Type, object[], object> _DynamicMock; + + public RhinoMockRepositoryProxy() + { + // We may consider allowing the consumer to pass in the MockRepository Type so we can avoid any possible Assembly conflict issues. + // (The assumption being that their test project already has a reference to Rhino.Mocks.) + // ex: var myclass = RhinoAutoMocker<MyClass>(MockMode.AAA, typeof(MockRepository) + var RhinoMocks = Assembly.Load("Rhino.Mocks"); + var mockRepositoryType = RhinoMocks.GetType("Rhino.Mocks.MockRepository"); + if (mockRepositoryType == null) throw new InvalidOperationException("Unable to find Type Rhino.Mocks.MockRepository in assembly " + RhinoMocks.Location); + + _instance = Activator.CreateInstance(mockRepositoryType); + var methodInfo = mockRepositoryType.GetMethod("DynamicMock", new[] { typeof(Type), typeof(object[]) }); + if (methodInfo == null) throw new InvalidOperationException("Unable to find method DynamicMock(Type, object[]) on MockRepository."); + _DynamicMock = (Func<Type, object[], object>)Delegate.CreateDelegate(typeof(Func<Type, object[], object>), _instance, methodInfo); + + methodInfo = mockRepositoryType.GetMethod("PartialMock", new[] { typeof(Type), typeof(object[]) }); + if (methodInfo == null) throw new InvalidOperationException("Unable to find method PartialMock(Type, object[] on MockRepository."); + _PartialMock = (Func<Type, object[], object>)Delegate.CreateDelegate(typeof(Func<Type, object[], object>), _instance, methodInfo); + + + methodInfo = mockRepositoryType.GetMethod("Replay", new[] { typeof(object) }); + if (methodInfo == null) throw new InvalidOperationException("Unable to find method Replay(object) on MockRepository."); + _Replay = (Action<object>)Delegate.CreateDelegate(typeof(Action<object>), _instance, methodInfo); + } + + public object DynamicMock(Type type) + { + return _DynamicMock(type, null); + } + + public object PartialMock(Type type, object[] args) + { + return _PartialMock(type, args); + } + + public void Replay(object mock) + { + _Replay(mock); + } + + } +} \ No newline at end of file Modified: trunk/Source/StructureMap.AutoMocking/ServiceLocator.cs =================================================================== --- trunk/Source/StructureMap.AutoMocking/ServiceLocator.cs 2008-12-17 16:41:58 UTC (rev 198) +++ trunk/Source/StructureMap.AutoMocking/ServiceLocator.cs 2008-12-19 02:41:25 UTC (rev 199) @@ -1,5 +1,4 @@ using System; -using Rhino.Mocks; namespace StructureMap.AutoMocking { @@ -12,20 +11,11 @@ public class RhinoMocksClassicServiceLocator : ServiceLocator { - private readonly MockRepository _mocks; + private readonly RhinoMockRepositoryProxy _mocks = new RhinoMockRepositoryProxy(); - public RhinoMocksClassicServiceLocator(MockRepository repository) - { - _mocks = repository; - } - - public RhinoMocksClassicServiceLocator() : this(new MockRepository()) - { - } - public T Service<T>() where T : class { - return _mocks.DynamicMock<T>(); + return (T)_mocks.DynamicMock(typeof(T)); } public object Service(Type serviceType) @@ -35,31 +25,33 @@ public T PartialMock<T>(params object[] args) where T : class { - return _mocks.PartialMock<T>(args); + return (T)_mocks.PartialMock(typeof(T), args); } } public class RhinoMocksAAAServiceLocator : ServiceLocator { + private readonly RhinoMockRepositoryProxy _mocks = new RhinoMockRepositoryProxy(); + public T Service<T>() where T : class { - return MockRepository.GenerateMock<T>(); + var instance = (T)_mocks.DynamicMock(typeof (T)); + _mocks.Replay(instance); + return instance; } public object Service(Type serviceType) { - var mock = new MockRepository().DynamicMock(serviceType); - mock.Replay(); - - return mock; + var instance = _mocks.DynamicMock(serviceType); + _mocks.Replay(instance); + return instance; } public T PartialMock<T>(params object[] args) where T : class { - T mock = new MockRepository().PartialMock<T>(args); - mock.Replay(); - - return mock; + var instance = (T)_mocks.PartialMock(typeof(T), args); + _mocks.Replay(instance); + return instance; } } } \ No newline at end of file Modified: trunk/Source/StructureMap.AutoMocking/StructureMap.AutoMocking.csproj =================================================================== --- trunk/Source/StructureMap.AutoMocking/StructureMap.AutoMocking.csproj 2008-12-17 16:41:58 UTC (rev 198) +++ trunk/Source/StructureMap.AutoMocking/StructureMap.AutoMocking.csproj 2008-12-19 02:41:25 UTC (rev 199) @@ -53,10 +53,6 @@ <DocumentationFile>bin\Release\StructureMap.AutoMocking.XML</DocumentationFile> </PropertyGroup> <ItemGroup> - <Reference Include="Rhino.Mocks, Version=3.5.0.1337, Culture=neutral, PublicKeyToken=0b3305902db7183f, processorArchitecture=MSIL"> - <SpecificVersion>False</SpecificVersion> - <HintPath>..\..\bin\Rhino.Mocks.dll</HintPath> - </Reference> <Reference Include="System" /> <Reference Include="System.Core"> <RequiredTargetFramework>3.5</RequiredTargetFramework> @@ -72,6 +68,7 @@ <Compile Include="AutoMocker.cs" /> <Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="RhinoAutoMocker.cs" /> + <Compile Include="RhinoMockRepositoryProxy.cs" /> <Compile Include="ServiceLocator.cs" /> </ItemGroup> <ItemGroup> @@ -110,4 +107,4 @@ <Target Name="AfterBuild"> </Target> --> -</Project> \ No newline at end of file +</Project> Added: trunk/Source/StructureMap.Testing/AutoMocking/RhinoMockRepositoryProxyTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/AutoMocking/RhinoMockRepositoryProxyTester.cs (rev 0) +++ trunk/Source/StructureMap.Testing/AutoMocking/RhinoMockRepositoryProxyTester.cs 2008-12-19 02:41:25 UTC (rev 199) @@ -0,0 +1,66 @@ +using NUnit.Framework; +using StructureMap.AutoMocking; +using Rhino.Mocks; + +namespace StructureMap.Testing.AutoMocking +{ + [TestFixture] + public class RhinoMockRepositoryProxyTester + { + [Test] + public void can_make_dynamic_mocks() + { + var mockRepository = new RhinoMockRepositoryProxy(); + var fooMock = mockRepository.DynamicMock(typeof(ITestMocks)); + + fooMock.ShouldNotBeNull(); + } + + [Test] + public void can_put_mock_in_replay_mode() + { + var mockRepository = new RhinoMockRepositoryProxy(); + var test = (ITestMocks)mockRepository.DynamicMock(typeof(ITestMocks)); + + mockRepository.Replay(test); + + test.Stub(t => t.Answer()).Return("YES"); + test.ShouldNotBeNull(); + test.Answer().ShouldEqual("YES"); + } + + [Test] + public void can_make_partial_mocks() + { + var mockRepository = new RhinoMockRepositoryProxy(); + var testPartials = (TestPartials)mockRepository.PartialMock(typeof(TestPartials), new object[0]); + + testPartials.ShouldNotBeNull(); + mockRepository.Replay(testPartials); + testPartials.Concrete().ShouldEqual("Concrete"); + testPartials.Virtual().ShouldEqual("Virtual"); + + testPartials.Stub(t => t.Virtual()).Return("MOCKED!"); + testPartials.Virtual().ShouldEqual("MOCKED!"); + } + + } + + public interface ITestMocks + { + string Answer(); + } + + public class TestPartials + { + public string Concrete() + { + return "Concrete"; + } + + public virtual string Virtual() + { + return "Virtual"; + } + } +} \ No newline at end of file Modified: trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj =================================================================== --- trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj 2008-12-17 16:41:58 UTC (rev 198) +++ trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj 2008-12-19 02:41:25 UTC (rev 199) @@ -170,6 +170,7 @@ <SubType>Code</SubType> </Compile> <Compile Include="AutoMocking\RhinoAutoMockerTester.cs" /> + <Compile Include="AutoMocking\RhinoMockRepositoryProxyTester.cs" /> <Compile Include="BuildSessionTester.cs" /> <Compile Include="Configuration\ConfigurationParserBuilderTester.cs" /> <Compile Include="Configuration\ConfigurationParserTester.cs" /> @@ -445,4 +446,4 @@ <PostBuildEvent> </PostBuildEvent> </PropertyGroup> -</Project> \ No newline at end of file +</Project> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |