Merge branch 'master' of git://github.com/kayone/NzbDrone

This commit is contained in:
Mark McDowall 2011-04-08 09:54:03 -07:00
commit 4426072232
5 changed files with 214 additions and 7 deletions

View File

@ -16,6 +16,7 @@ namespace AutoMoq
{ {
private IUnityContainer container; private IUnityContainer container;
private IDictionary<Type, object> registeredMocks; private IDictionary<Type, object> registeredMocks;
internal Type ResolveType = null;
public AutoMoqer() public AutoMoqer()
{ {
@ -29,11 +30,15 @@ namespace AutoMoq
public virtual T Resolve<T>() public virtual T Resolve<T>()
{ {
return container.Resolve<T>(); ResolveType = typeof(T);
var result = container.Resolve<T>();
ResolveType = null;
return result;
} }
public virtual Mock<T> GetMock<T>() where T : class public virtual Mock<T> GetMock<T>() where T : class
{ {
ResolveType = null;
var type = GetTheMockType<T>(); var type = GetTheMockType<T>();
if (GetMockHasNotBeenCalledForThisType(type)) if (GetMockHasNotBeenCalledForThisType(type))
CreateANewMockAndRegisterIt<T>(type); CreateANewMockAndRegisterIt<T>(type);

View File

@ -0,0 +1,196 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using AutoMoq;
using FizzWare.NBuilder;
using Gallio.Framework;
using MbUnit.Framework;
using MbUnit.Framework.ContractVerifiers;
using Moq;
using Ninject;
using Ninject.Moq;
using NzbDrone.Core.Providers;
using NzbDrone.Core.Repository;
using NzbDrone.Core.Repository.Quality;
using SubSonic.Repository;
using TvdbLib.Data;
using SubSonic.Extensions;
namespace NzbDrone.Core.Test
{
[TestFixture]
// ReSharper disable InconsistentNaming
public class AutoMoqerTest
{
[Test]
public void GetMock_on_interface_returns_mock()
{
//Arrange
var mocker = new AutoMoqer();
//Act
var mock = mocker.GetMock<IDependency>();
//Assert
Assert.IsNotNull(mock);
}
[Test]
public void GetMock_on_concrete_returns_mock()
{
//Arrange
var mocker = new AutoMoqer();
//Act
var mock = mocker.GetMock<ConcreteClass>();
//Assert
Assert.IsNotNull(mock);
}
[Test]
public void Resolve_doesnt_return_mock()
{
//Arrange
var mocker = new AutoMoqer();
//Act
var result = mocker.Resolve<ConcreteClass>().Do();
//Assert
Assert.AreEqual("hello", result);
}
[Test]
public void Resolve_with_dependency_doesnt_return_mock()
{
//Arrange
var mocker = new AutoMoqer();
//Act
var result = mocker.Resolve<VirtualDependency>().VirtualMethod();
//Assert
Assert.AreEqual("hello", result);
}
[Test]
public void Resolve_with_mocked_dependency_uses_mock()
{
//Arrange
var mocker = new AutoMoqer();
mocker.GetMock<VirtualDependency>()
.Setup(m => m.VirtualMethod())
.Returns("mocked");
//Act
var result = mocker.Resolve<ClassWithVirtualDependencies>().CallVirtualChild();
//Assert
Assert.AreEqual("mocked", result);
}
[Test]
public void Resolve_with_unbound_concerete_dependency_uses_mock()
{
//Arrange
var mocker = new AutoMoqer();
//Act
var result = mocker.Resolve<ClassWithVirtualDependencies>().CallVirtualChild();
var mockedResult = new Mock<VirtualDependency>().Object.VirtualMethod();
//Assert
Assert.AreEqual(mockedResult, result);
}
[Test]
public void Resolve_with_constant_concerete_dependency_uses_constant()
{
//Arrange
var mocker = new AutoMoqer();
var constant = new VirtualDependency() { PropValue = Guid.NewGuid().ToString() };
mocker.SetConstant(constant);
//Act
var result = mocker.Resolve<ClassWithVirtualDependencies>().GetVirtualProperty();
//Assert
Assert.AreEqual(constant.PropValue, result);
}
}
public class ConcreteClass
{
public string Do()
{
return "hello";
}
}
public class Dependency : IDependency { }
public interface IDependency { }
public class ClassWithDependencies
{
public IDependency Dependency { get; set; }
public ClassWithDependencies(IDependency dependency)
{
Dependency = dependency;
}
}
public class ClassWithVirtualDependencies
{
private readonly VirtualDependency _virtualDependency;
public IDependency Dependency { get; set; }
public ClassWithVirtualDependencies(IDependency dependency, VirtualDependency virtualDependency)
{
_virtualDependency = virtualDependency;
Dependency = dependency;
}
public string CallVirtualChild()
{
return _virtualDependency.VirtualMethod();
}
public string GetVirtualProperty()
{
return _virtualDependency.PropValue;
}
}
public class VirtualDependency
{
private readonly IDependency _dependency;
public string PropValue { get; set; }
public VirtualDependency() { }
public VirtualDependency(IDependency dependency)
{
_dependency = dependency;
}
public virtual string VirtualMethod()
{
return "hello";
}
}
}

View File

@ -38,12 +38,14 @@ namespace AutoMoq.Unity
private bool AMockObjectShouldBeCreatedForThisType(Type type) private bool AMockObjectShouldBeCreatedForThisType(Type type)
{ {
return TypeIsNotRegistered(type) && type.IsInterface; var mocker = container.Resolve<AutoMoqer>();
return TypeIsNotRegistered(type) && (mocker.ResolveType == null || mocker.ResolveType != type);
//return TypeIsNotRegistered(type) && type.IsInterface;
} }
private static Type GetTheTypeFromTheBuilderContext(IBuilderContext context) private static Type GetTheTypeFromTheBuilderContext(IBuilderContext context)
{ {
return ((NamedTypeBuildKey) context.OriginalBuildKey).Type; return ((NamedTypeBuildKey)context.OriginalBuildKey).Type;
} }
private bool TypeIsNotRegistered(Type type) private bool TypeIsNotRegistered(Type type)
@ -60,19 +62,19 @@ namespace AutoMoq.Unity
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

View File

@ -86,6 +86,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="AutoMoq\AutoMoqer.cs" /> <Compile Include="AutoMoq\AutoMoqer.cs" />
<Compile Include="AutoMoq\AutoMoqerTest.cs" />
<Compile Include="AutoMoq\Unity\AutoMockingBuilderStrategy.cs"> <Compile Include="AutoMoq\Unity\AutoMockingBuilderStrategy.cs">
<SubType>Code</SubType> <SubType>Code</SubType>
</Compile> </Compile>

View File

@ -14,7 +14,10 @@ namespace NzbDrone.Core.Providers
private IRepository _sonicRepo; private IRepository _sonicRepo;
private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
public QualityProvider() { } public QualityProvider()
{
}
public QualityProvider(IRepository sonicRepo) public QualityProvider(IRepository sonicRepo)
{ {