From ccbbad54cee408d05879cf57db184fb72e3ad0ec Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Fri, 22 Apr 2011 12:15:47 -0700 Subject: [PATCH] Added default mock behavior to automocker --- NzbDrone.Core.Test/AutoMoq/AutoMoqer.cs | 21 +++++++++++++++---- .../Unity/AutoMockingBuilderStrategy.cs | 9 ++++---- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/NzbDrone.Core.Test/AutoMoq/AutoMoqer.cs b/NzbDrone.Core.Test/AutoMoq/AutoMoqer.cs index b5535fb24..91803a469 100644 --- a/NzbDrone.Core.Test/AutoMoq/AutoMoqer.cs +++ b/NzbDrone.Core.Test/AutoMoq/AutoMoqer.cs @@ -14,6 +14,7 @@ namespace AutoMoq { public class AutoMoqer { + internal readonly MockBehavior DefaultBehavior = MockBehavior.Default; internal Type ResolveType; private IUnityContainer container; private IDictionary registeredMocks; @@ -23,6 +24,13 @@ namespace AutoMoq SetupAutoMoqer(new UnityContainer()); } + public AutoMoqer(MockBehavior defaultBehavior) + { + DefaultBehavior = defaultBehavior; + SetupAutoMoqer(new UnityContainer()); + + } + internal AutoMoqer(IUnityContainer container) { SetupAutoMoqer(container); @@ -37,7 +45,12 @@ namespace AutoMoq return result; } - public virtual Mock GetMock(MockBehavior behavior = MockBehavior.Default) where T : class + public virtual Mock GetMock() where T : class + { + return GetMock(DefaultBehavior); + } + + public virtual Mock GetMock(MockBehavior behavior) where T : class { ResolveType = null; var type = GetTheMockType(); @@ -112,10 +125,10 @@ namespace AutoMoq private void SetupAutoMoqer(IUnityContainer container) { this.container = container; - registeredMocks = new Dictionary(); - - AddTheAutoMockingContainerExtensionToTheContainer(container); container.RegisterInstance(this); + + registeredMocks = new Dictionary(); + AddTheAutoMockingContainerExtensionToTheContainer(container); } private static void AddTheAutoMockingContainerExtensionToTheContainer(IUnityContainer container) diff --git a/NzbDrone.Core.Test/AutoMoq/Unity/AutoMockingBuilderStrategy.cs b/NzbDrone.Core.Test/AutoMoq/Unity/AutoMockingBuilderStrategy.cs index 26297916e..c89ab8491 100644 --- a/NzbDrone.Core.Test/AutoMoq/Unity/AutoMockingBuilderStrategy.cs +++ b/NzbDrone.Core.Test/AutoMoq/Unity/AutoMockingBuilderStrategy.cs @@ -16,7 +16,8 @@ namespace AutoMoq.Unity public AutoMockingBuilderStrategy(IEnumerable registeredTypes, IUnityContainer container) { - mockFactory = new MockFactory(MockBehavior.Loose); + var autoMoqer = container.Resolve(); + mockFactory = new MockFactory(autoMoqer.DefaultBehavior); this.registeredTypes = registeredTypes; this.container = container; } @@ -62,19 +63,19 @@ namespace AutoMoq.Unity private Mock InvokeTheMockCreationMethod(MethodInfo createMethod) { - return (Mock) createMethod.Invoke(mockFactory, new object[] {new List().ToArray()}); + return (Mock)createMethod.Invoke(mockFactory, new object[] { new List().ToArray() }); } private MethodInfo GenerateAnInterfaceMockCreationMethod(Type type) { var createMethodWithNoParameters = mockFactory.GetType().GetMethod("Create", EmptyArgumentList()); - return createMethodWithNoParameters.MakeGenericMethod(new[] {type}); + return createMethodWithNoParameters.MakeGenericMethod(new[] { type }); } private static Type[] EmptyArgumentList() { - return new[] {typeof (object[])}; + return new[] { typeof(object[]) }; } #endregion