mirror of
https://github.com/Radarr/Radarr.git
synced 2024-09-11 12:02:35 +02:00
Added default mock behavior to automocker
This commit is contained in:
parent
fd32a04d45
commit
ccbbad54ce
@ -14,6 +14,7 @@ namespace AutoMoq
|
|||||||
{
|
{
|
||||||
public class AutoMoqer
|
public class AutoMoqer
|
||||||
{
|
{
|
||||||
|
internal readonly MockBehavior DefaultBehavior = MockBehavior.Default;
|
||||||
internal Type ResolveType;
|
internal Type ResolveType;
|
||||||
private IUnityContainer container;
|
private IUnityContainer container;
|
||||||
private IDictionary<Type, object> registeredMocks;
|
private IDictionary<Type, object> registeredMocks;
|
||||||
@ -23,6 +24,13 @@ public AutoMoqer()
|
|||||||
SetupAutoMoqer(new UnityContainer());
|
SetupAutoMoqer(new UnityContainer());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public AutoMoqer(MockBehavior defaultBehavior)
|
||||||
|
{
|
||||||
|
DefaultBehavior = defaultBehavior;
|
||||||
|
SetupAutoMoqer(new UnityContainer());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
internal AutoMoqer(IUnityContainer container)
|
internal AutoMoqer(IUnityContainer container)
|
||||||
{
|
{
|
||||||
SetupAutoMoqer(container);
|
SetupAutoMoqer(container);
|
||||||
@ -37,7 +45,12 @@ public virtual T Resolve<T>()
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual Mock<T> GetMock<T>(MockBehavior behavior = MockBehavior.Default) where T : class
|
public virtual Mock<T> GetMock<T>() where T : class
|
||||||
|
{
|
||||||
|
return GetMock<T>(DefaultBehavior);
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual Mock<T> GetMock<T>(MockBehavior behavior) where T : class
|
||||||
{
|
{
|
||||||
ResolveType = null;
|
ResolveType = null;
|
||||||
var type = GetTheMockType<T>();
|
var type = GetTheMockType<T>();
|
||||||
@ -112,10 +125,10 @@ public void VerifyAllMocks()
|
|||||||
private void SetupAutoMoqer(IUnityContainer container)
|
private void SetupAutoMoqer(IUnityContainer container)
|
||||||
{
|
{
|
||||||
this.container = container;
|
this.container = container;
|
||||||
registeredMocks = new Dictionary<Type, object>();
|
|
||||||
|
|
||||||
AddTheAutoMockingContainerExtensionToTheContainer(container);
|
|
||||||
container.RegisterInstance(this);
|
container.RegisterInstance(this);
|
||||||
|
|
||||||
|
registeredMocks = new Dictionary<Type, object>();
|
||||||
|
AddTheAutoMockingContainerExtensionToTheContainer(container);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void AddTheAutoMockingContainerExtensionToTheContainer(IUnityContainer container)
|
private static void AddTheAutoMockingContainerExtensionToTheContainer(IUnityContainer container)
|
||||||
|
@ -16,7 +16,8 @@ internal class AutoMockingBuilderStrategy : BuilderStrategy
|
|||||||
|
|
||||||
public AutoMockingBuilderStrategy(IEnumerable<Type> registeredTypes, IUnityContainer container)
|
public AutoMockingBuilderStrategy(IEnumerable<Type> registeredTypes, IUnityContainer container)
|
||||||
{
|
{
|
||||||
mockFactory = new MockFactory(MockBehavior.Loose);
|
var autoMoqer = container.Resolve<AutoMoqer>();
|
||||||
|
mockFactory = new MockFactory(autoMoqer.DefaultBehavior);
|
||||||
this.registeredTypes = registeredTypes;
|
this.registeredTypes = registeredTypes;
|
||||||
this.container = container;
|
this.container = container;
|
||||||
}
|
}
|
||||||
@ -62,19 +63,19 @@ private Mock CreateAMockObject(Type type)
|
|||||||
|
|
||||||
private Mock InvokeTheMockCreationMethod(MethodInfo createMethod)
|
private Mock InvokeTheMockCreationMethod(MethodInfo createMethod)
|
||||||
{
|
{
|
||||||
return (Mock) createMethod.Invoke(mockFactory, new object[] {new List<object>().ToArray()});
|
return (Mock)createMethod.Invoke(mockFactory, new object[] { new List<object>().ToArray() });
|
||||||
}
|
}
|
||||||
|
|
||||||
private MethodInfo GenerateAnInterfaceMockCreationMethod(Type type)
|
private MethodInfo GenerateAnInterfaceMockCreationMethod(Type type)
|
||||||
{
|
{
|
||||||
var createMethodWithNoParameters = mockFactory.GetType().GetMethod("Create", EmptyArgumentList());
|
var createMethodWithNoParameters = mockFactory.GetType().GetMethod("Create", EmptyArgumentList());
|
||||||
|
|
||||||
return createMethodWithNoParameters.MakeGenericMethod(new[] {type});
|
return createMethodWithNoParameters.MakeGenericMethod(new[] { type });
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Type[] EmptyArgumentList()
|
private static Type[] EmptyArgumentList()
|
||||||
{
|
{
|
||||||
return new[] {typeof (object[])};
|
return new[] { typeof(object[]) };
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
Loading…
Reference in New Issue
Block a user