From dcea725bfc65d2f7a64727f1dda195c7179ef426 Mon Sep 17 00:00:00 2001 From: Andre Azevedo Date: Tue, 9 Aug 2011 01:53:59 -0300 Subject: [PATCH] Adding infrastructure unit testing. related #1 --- .../CryptographyImplementationTest.cs | 6 +- .../InversionOfControl/IoCTest.cs | 51 ++++++++++++ .../Infrastructure/Logging/LoggerTest.cs | 78 +++++++++++++++++++ .../UnitOfWork/UnitOfWorkManagerTest.cs | 40 ++++++++++ .../PetStore.Core.Test.csproj | 7 ++ 5 files changed, 179 insertions(+), 3 deletions(-) create mode 100644 src/Test/PetStore.Core.Test/Infrastructure/InversionOfControl/IoCTest.cs create mode 100644 src/Test/PetStore.Core.Test/Infrastructure/Logging/LoggerTest.cs create mode 100644 src/Test/PetStore.Core.Test/Infrastructure/UnitOfWork/UnitOfWorkManagerTest.cs diff --git a/src/Test/PetStore.Core.Test/Infrastructure/Crypt/Implementation/CryptographyImplementationTest.cs b/src/Test/PetStore.Core.Test/Infrastructure/Crypt/Implementation/CryptographyImplementationTest.cs index adf3504..bd6dc3d 100644 --- a/src/Test/PetStore.Core.Test/Infrastructure/Crypt/Implementation/CryptographyImplementationTest.cs +++ b/src/Test/PetStore.Core.Test/Infrastructure/Crypt/Implementation/CryptographyImplementationTest.cs @@ -11,7 +11,7 @@ namespace PetStore.Core.Test.Infrastructure.Crypt.Implementation public class CryptographyImplementationTest { [Test] - public void EncryptAndDecryptShouldReturnCorrectResult() + public void EncryptAndDecrypt_ShouldReturnCorrectResult() { const string value = "Téstê açentuação 123?!."; @@ -25,7 +25,7 @@ public void EncryptAndDecryptShouldReturnCorrectResult() } [Test] - public void EncryptAndDecryptNumberShouldReturnCorrectResult() + public void EncryptAndDecryptNumber_ShouldReturnCorrectResult() { const string value = "1"; @@ -39,7 +39,7 @@ public void EncryptAndDecryptNumberShouldReturnCorrectResult() } [Test] - public void ComputeHashShouldReturnCorrectValue() + public void ComputeHash_ShouldReturnCorrectValue() { const string value1 = "123456"; const string value2 = "123457"; diff --git a/src/Test/PetStore.Core.Test/Infrastructure/InversionOfControl/IoCTest.cs b/src/Test/PetStore.Core.Test/Infrastructure/InversionOfControl/IoCTest.cs new file mode 100644 index 0000000..d8e9104 --- /dev/null +++ b/src/Test/PetStore.Core.Test/Infrastructure/InversionOfControl/IoCTest.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Moq; +using NUnit.Framework; +using PetStore.Core.Infrastructure.InversionOfControl; +using PetStore.Core.Infrastructure.Logging; +using PetStore.Core.Infrastructure.UnitOfWork; +using PetStore.Core.Test.Infrastructure.Logging; +using PetStore.Core.Test.Infrastructure.UnitOfWork; + +namespace PetStore.Core.Test.Infrastructure.InversionOfControl +{ + [TestFixture] + public class IoCTest + { + #region Test Methods + + [Test] + public void InitializeShouldNotThrowException() + { + IDependencyResolver dependencyResolverMock = GetIDependencyResolverMock(); + IoC.Initialize(dependencyResolverMock); + } + + [Test] + public void ResolveShouldReturnCorrectResult() + { + IDependencyResolver dependencyResolverMock = GetIDependencyResolverMock(); + IoC.Initialize(dependencyResolverMock); + ILogger logger = IoC.Resolve(); + Assert.IsNotNull(logger); + } + #endregion + + #region Static Methods + + public static IDependencyResolver GetIDependencyResolverMock() + { + Mock dependencyResolver = new Mock(); + + dependencyResolver.Setup(d => d.Resolve()).Returns(LoggerTest.GetILogMock()); + dependencyResolver.Setup(d => d.Resolve()).Returns(UnitOfWorkManagerTest.GetUnitOfWorkMock()); + + return dependencyResolver.Object; + } + + #endregion + } +} diff --git a/src/Test/PetStore.Core.Test/Infrastructure/Logging/LoggerTest.cs b/src/Test/PetStore.Core.Test/Infrastructure/Logging/LoggerTest.cs new file mode 100644 index 0000000..2413664 --- /dev/null +++ b/src/Test/PetStore.Core.Test/Infrastructure/Logging/LoggerTest.cs @@ -0,0 +1,78 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using NUnit.Framework; +using PetStore.Core.Infrastructure.InversionOfControl; +using PetStore.Core.Infrastructure.Logging; +using PetStore.Core.Test.Infrastructure.InversionOfControl; +using PetStore.TestBase.Mocks.Core.Infrastructure.Logging; + +namespace PetStore.Core.Test.Infrastructure.Logging +{ + [TestFixture] + public class LoggerTest + { + #region Test Methods + + [Test] + public void InfoShouldReturnCorrectResult() + { + const string msg = "Info Message 1"; + IoC.Initialize(IoCTest.GetIDependencyResolverMock()); + int numOfLoggedMessagesBefore = LoggerMock.Messages.Count; + Logger.Info(msg); + int numOfLoggedMessagesAfter = LoggerMock.Messages.Count; + Assert.AreEqual(numOfLoggedMessagesBefore + 1, numOfLoggedMessagesAfter); + Assert.AreEqual(msg, LoggerMock.Messages.Last()); + } + + [Test] + public void WaringShouldReturnCorrectResult() + { + const string msg = "Warning Message 1"; + IoC.Initialize(IoCTest.GetIDependencyResolverMock()); + int numOfLoggedMessagesBefore = LoggerMock.Messages.Count; + Logger.Warning(msg); + int numOfLoggedMessagesAfter = LoggerMock.Messages.Count; + Assert.AreEqual(numOfLoggedMessagesBefore + 1, numOfLoggedMessagesAfter); + Assert.AreEqual(msg, LoggerMock.Messages.Last()); + } + + [Test] + public void ErrorShouldReturnCorrectResult() + { + const string msg = "Error Message 1"; + IoC.Initialize(IoCTest.GetIDependencyResolverMock()); + int numOfLoggedMessagesBefore = LoggerMock.Messages.Count; + Logger.Error(msg); + int numOfLoggedMessagesAfter = LoggerMock.Messages.Count; + Assert.AreEqual(numOfLoggedMessagesBefore + 1, numOfLoggedMessagesAfter); + Assert.AreEqual(msg, LoggerMock.Messages.Last()); + } + + [Test] + public void ExceptionShouldReturnCorrectResult() + { + const string msg = "Exception Message 1"; + ApplicationException applicationException = new ApplicationException(msg); + IoC.Initialize(IoCTest.GetIDependencyResolverMock()); + int numOfLoggedMessagesBefore = LoggerMock.Messages.Count; + Logger.Exception(applicationException); + int numOfLoggedMessagesAfter = LoggerMock.Messages.Count; + Assert.AreEqual(numOfLoggedMessagesBefore + 1, numOfLoggedMessagesAfter); + Assert.AreEqual(msg, LoggerMock.Messages.Last()); + } + + #endregion + + #region Static Methods + + public static ILogger GetILogMock() + { + return new LoggerMock(); + } + + #endregion + } +} diff --git a/src/Test/PetStore.Core.Test/Infrastructure/UnitOfWork/UnitOfWorkManagerTest.cs b/src/Test/PetStore.Core.Test/Infrastructure/UnitOfWork/UnitOfWorkManagerTest.cs new file mode 100644 index 0000000..3aab375 --- /dev/null +++ b/src/Test/PetStore.Core.Test/Infrastructure/UnitOfWork/UnitOfWorkManagerTest.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Moq; +using NUnit.Framework; +using PetStore.Core.Infrastructure.InversionOfControl; +using PetStore.Core.Infrastructure.UnitOfWork; +using PetStore.Core.Test.Infrastructure.InversionOfControl; + +namespace PetStore.Core.Test.Infrastructure.UnitOfWork +{ + [TestFixture] + public class UnitOfWorkManagerTest + { + #region Test Methods + + [Test] + public void BeginShouldReturnCorrectResult() + { + IoC.Initialize(IoCTest.GetIDependencyResolverMock()); + + IUnitOfWork unitOfWork = UnitOfWorkManager.Begin(); + + Assert.IsNotNull(unitOfWork); + } + + #endregion + + #region Static Methods + + public static IUnitOfWork GetUnitOfWorkMock() + { + Mock unitOfWorkMock = new Mock(); + return unitOfWorkMock.Object; + } + + #endregion + } +} diff --git a/src/Test/PetStore.Core.Test/PetStore.Core.Test.csproj b/src/Test/PetStore.Core.Test/PetStore.Core.Test.csproj index 973998a..b7711d1 100644 --- a/src/Test/PetStore.Core.Test/PetStore.Core.Test.csproj +++ b/src/Test/PetStore.Core.Test/PetStore.Core.Test.csproj @@ -49,6 +49,9 @@ + + + @@ -56,6 +59,10 @@ {FF79E5EE-F489-4239-B935-DA2C06848BFA} PetStore.Core + + {BE801B36-49C9-4770-952C-76259F81F5FF} + PetStore.TestBase +