wrote automoqer unit test

pull/6/head
kay.one 14 years ago
parent 7b4a92fbe6
commit 53607d89fe

@ -16,6 +16,7 @@ namespace AutoMoq
{
private IUnityContainer container;
private IDictionary<Type, object> registeredMocks;
internal Type ResolveType = null;
public AutoMoqer()
{
@ -29,11 +30,15 @@ namespace AutoMoq
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
{
ResolveType = null;
var type = GetTheMockType<T>();
if (GetMockHasNotBeenCalledForThisType(type))
CreateANewMockAndRegisterIt<T>(type);

@ -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";
}
}
}

@ -38,7 +38,9 @@ namespace AutoMoq.Unity
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)

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

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

Loading…
Cancel
Save