2011-05-22 16:53:21 +00:00
|
|
|
|
// ReSharper disable RedundantUsingDirective
|
|
|
|
|
using System;
|
2011-04-07 03:34:48 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using Microsoft.Practices.ObjectBuilder2;
|
|
|
|
|
using Microsoft.Practices.Unity;
|
|
|
|
|
using Moq;
|
|
|
|
|
|
|
|
|
|
namespace AutoMoq.Unity
|
|
|
|
|
{
|
|
|
|
|
internal class AutoMockingBuilderStrategy : BuilderStrategy
|
|
|
|
|
{
|
2011-05-19 03:55:35 +00:00
|
|
|
|
private readonly IUnityContainer _container;
|
|
|
|
|
private readonly MockRepository _mockFactory;
|
|
|
|
|
private readonly IEnumerable<Type> _registeredTypes;
|
2011-04-07 03:34:48 +00:00
|
|
|
|
|
|
|
|
|
public AutoMockingBuilderStrategy(IEnumerable<Type> registeredTypes, IUnityContainer container)
|
|
|
|
|
{
|
2011-04-22 19:15:47 +00:00
|
|
|
|
var autoMoqer = container.Resolve<AutoMoqer>();
|
2011-05-19 03:55:35 +00:00
|
|
|
|
_mockFactory = new MockRepository(autoMoqer.DefaultBehavior);
|
|
|
|
|
_registeredTypes = registeredTypes;
|
|
|
|
|
_container = container;
|
2011-04-07 03:34:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void PreBuildUp(IBuilderContext context)
|
|
|
|
|
{
|
2011-05-19 03:55:35 +00:00
|
|
|
|
var autoMoqer = _container.Resolve<AutoMoqer>();
|
2011-04-07 03:34:48 +00:00
|
|
|
|
|
|
|
|
|
var type = GetTheTypeFromTheBuilderContext(context);
|
|
|
|
|
if (AMockObjectShouldBeCreatedForThisType(type))
|
|
|
|
|
{
|
|
|
|
|
var mock = CreateAMockObject(type);
|
|
|
|
|
context.Existing = mock.Object;
|
|
|
|
|
autoMoqer.SetMock(type, mock);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region private methods
|
|
|
|
|
|
|
|
|
|
private bool AMockObjectShouldBeCreatedForThisType(Type type)
|
|
|
|
|
{
|
2011-05-19 03:55:35 +00:00
|
|
|
|
var mocker = _container.Resolve<AutoMoqer>();
|
2011-04-08 06:50:30 +00:00
|
|
|
|
return TypeIsNotRegistered(type) && (mocker.ResolveType == null || mocker.ResolveType != type);
|
|
|
|
|
//return TypeIsNotRegistered(type) && type.IsInterface;
|
2011-04-07 03:34:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Type GetTheTypeFromTheBuilderContext(IBuilderContext context)
|
|
|
|
|
{
|
2011-04-10 02:44:01 +00:00
|
|
|
|
return (context.OriginalBuildKey).Type;
|
2011-04-07 03:34:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool TypeIsNotRegistered(Type type)
|
|
|
|
|
{
|
2011-05-19 03:55:35 +00:00
|
|
|
|
return _registeredTypes.Any(x => x.Equals(type)) == false;
|
2011-04-07 03:34:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Mock CreateAMockObject(Type type)
|
|
|
|
|
{
|
|
|
|
|
var createMethod = GenerateAnInterfaceMockCreationMethod(type);
|
|
|
|
|
|
|
|
|
|
return InvokeTheMockCreationMethod(createMethod);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Mock InvokeTheMockCreationMethod(MethodInfo createMethod)
|
|
|
|
|
{
|
2011-05-19 03:55:35 +00:00
|
|
|
|
return (Mock)createMethod.Invoke(_mockFactory, new object[] { new List<object>().ToArray() });
|
2011-04-07 03:34:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private MethodInfo GenerateAnInterfaceMockCreationMethod(Type type)
|
|
|
|
|
{
|
2011-05-19 03:55:35 +00:00
|
|
|
|
var createMethodWithNoParameters = _mockFactory.GetType().GetMethod("Create", EmptyArgumentList());
|
2011-04-07 03:34:48 +00:00
|
|
|
|
|
2011-04-22 19:15:47 +00:00
|
|
|
|
return createMethodWithNoParameters.MakeGenericMethod(new[] { type });
|
2011-04-07 03:34:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Type[] EmptyArgumentList()
|
|
|
|
|
{
|
2011-04-22 19:15:47 +00:00
|
|
|
|
return new[] { typeof(object[]) };
|
2011-04-07 03:34:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|