Skip to content

Commit

Permalink
Adding infrastructure unit testing. related #1
Browse files Browse the repository at this point in the history
  • Loading branch information
andreazevedo committed Aug 9, 2011
1 parent 40a67c3 commit dcea725
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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?!.";

Expand All @@ -25,7 +25,7 @@ public void EncryptAndDecryptShouldReturnCorrectResult()
}

[Test]
public void EncryptAndDecryptNumberShouldReturnCorrectResult()
public void EncryptAndDecryptNumber_ShouldReturnCorrectResult()
{
const string value = "1";

Expand All @@ -39,7 +39,7 @@ public void EncryptAndDecryptNumberShouldReturnCorrectResult()
}

[Test]
public void ComputeHashShouldReturnCorrectValue()
public void ComputeHash_ShouldReturnCorrectValue()
{
const string value1 = "123456";
const string value2 = "123457";
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ILogger>();
Assert.IsNotNull(logger);
}
#endregion

#region Static Methods

public static IDependencyResolver GetIDependencyResolverMock()
{
Mock<IDependencyResolver> dependencyResolver = new Mock<IDependencyResolver>();

dependencyResolver.Setup(d => d.Resolve<ILogger>()).Returns(LoggerTest.GetILogMock());
dependencyResolver.Setup(d => d.Resolve<IUnitOfWork>()).Returns(UnitOfWorkManagerTest.GetUnitOfWorkMock());

return dependencyResolver.Object;
}

#endregion
}
}
78 changes: 78 additions & 0 deletions src/Test/PetStore.Core.Test/Infrastructure/Logging/LoggerTest.cs
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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<IUnitOfWork> unitOfWorkMock = new Mock<IUnitOfWork>();
return unitOfWorkMock.Object;
}

#endregion
}
}
7 changes: 7 additions & 0 deletions src/Test/PetStore.Core.Test/PetStore.Core.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,20 @@
<Compile Include="Helper\CheckTest.cs" />
<Compile Include="Helper\DataValidatorTest.cs" />
<Compile Include="Infrastructure\Crypt\Implementation\CryptographyImplementationTest.cs" />
<Compile Include="Infrastructure\InversionOfControl\IoCTest.cs" />
<Compile Include="Infrastructure\Logging\LoggerTest.cs" />
<Compile Include="Infrastructure\UnitOfWork\UnitOfWorkManagerTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\PetStore.Core\PetStore.Core.csproj">
<Project>{FF79E5EE-F489-4239-B935-DA2C06848BFA}</Project>
<Name>PetStore.Core</Name>
</ProjectReference>
<ProjectReference Include="..\PetStore.TestBase\PetStore.TestBase.csproj">
<Project>{BE801B36-49C9-4770-952C-76259F81F5FF}</Project>
<Name>PetStore.TestBase</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand Down

0 comments on commit dcea725

Please sign in to comment.