From 53607d89fec49f5640022339208df573c39a4b9a Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Thu, 7 Apr 2011 23:50:30 -0700 Subject: [PATCH] wrote automoqer unit test --- NzbDrone.Core.Test/AutoMoq/AutoMoqer.cs | 7 +- NzbDrone.Core.Test/AutoMoq/AutoMoqerTest.cs | 196 ++++++++++++++++++ .../Unity/AutoMockingBuilderStrategy.cs | 12 +- NzbDrone.Core.Test/NzbDrone.Core.Test.csproj | 1 + NzbDrone.Core/Providers/QualityProvider.cs | 5 +- 5 files changed, 214 insertions(+), 7 deletions(-) create mode 100644 NzbDrone.Core.Test/AutoMoq/AutoMoqerTest.cs diff --git a/NzbDrone.Core.Test/AutoMoq/AutoMoqer.cs b/NzbDrone.Core.Test/AutoMoq/AutoMoqer.cs index 1fe168dcf..425ba1f8e 100644 --- a/NzbDrone.Core.Test/AutoMoq/AutoMoqer.cs +++ b/NzbDrone.Core.Test/AutoMoq/AutoMoqer.cs @@ -16,6 +16,7 @@ namespace AutoMoq { private IUnityContainer container; private IDictionary registeredMocks; + internal Type ResolveType = null; public AutoMoqer() { @@ -29,11 +30,15 @@ namespace AutoMoq public virtual T Resolve() { - return container.Resolve(); + ResolveType = typeof(T); + var result = container.Resolve(); + ResolveType = null; + return result; } public virtual Mock GetMock() where T : class { + ResolveType = null; var type = GetTheMockType(); if (GetMockHasNotBeenCalledForThisType(type)) CreateANewMockAndRegisterIt(type); diff --git a/NzbDrone.Core.Test/AutoMoq/AutoMoqerTest.cs b/NzbDrone.Core.Test/AutoMoq/AutoMoqerTest.cs new file mode 100644 index 000000000..19fecff58 --- /dev/null +++ b/NzbDrone.Core.Test/AutoMoq/AutoMoqerTest.cs @@ -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(); + + //Assert + Assert.IsNotNull(mock); + } + + [Test] + public void GetMock_on_concrete_returns_mock() + { + //Arrange + var mocker = new AutoMoqer(); + + //Act + var mock = mocker.GetMock(); + + //Assert + Assert.IsNotNull(mock); + } + + + [Test] + public void Resolve_doesnt_return_mock() + { + //Arrange + var mocker = new AutoMoqer(); + + //Act + var result = mocker.Resolve().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().VirtualMethod(); + + //Assert + Assert.AreEqual("hello", result); + } + + [Test] + public void Resolve_with_mocked_dependency_uses_mock() + { + //Arrange + var mocker = new AutoMoqer(); + + mocker.GetMock() + .Setup(m => m.VirtualMethod()) + .Returns("mocked"); + + //Act + var result = mocker.Resolve().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().CallVirtualChild(); + + var mockedResult = new Mock().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().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"; + } + } + +} diff --git a/NzbDrone.Core.Test/AutoMoq/Unity/AutoMockingBuilderStrategy.cs b/NzbDrone.Core.Test/AutoMoq/Unity/AutoMockingBuilderStrategy.cs index 81dcfd6b8..d6dff8997 100644 --- a/NzbDrone.Core.Test/AutoMoq/Unity/AutoMockingBuilderStrategy.cs +++ b/NzbDrone.Core.Test/AutoMoq/Unity/AutoMockingBuilderStrategy.cs @@ -38,12 +38,14 @@ namespace AutoMoq.Unity private bool AMockObjectShouldBeCreatedForThisType(Type type) { - return TypeIsNotRegistered(type) && type.IsInterface; + var mocker = container.Resolve(); + return TypeIsNotRegistered(type) && (mocker.ResolveType == null || mocker.ResolveType != type); + //return TypeIsNotRegistered(type) && type.IsInterface; } private static Type GetTheTypeFromTheBuilderContext(IBuilderContext context) { - return ((NamedTypeBuildKey) context.OriginalBuildKey).Type; + return ((NamedTypeBuildKey)context.OriginalBuildKey).Type; } private bool TypeIsNotRegistered(Type type) @@ -60,19 +62,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 diff --git a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj index 2dd08b835..26c8918d0 100644 --- a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj +++ b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj @@ -86,6 +86,7 @@ + Code diff --git a/NzbDrone.Core/Providers/QualityProvider.cs b/NzbDrone.Core/Providers/QualityProvider.cs index 6c2fe37da..99f716797 100644 --- a/NzbDrone.Core/Providers/QualityProvider.cs +++ b/NzbDrone.Core/Providers/QualityProvider.cs @@ -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) {