Unit Testing
The PDTec.IceNet.Sdk.Test.DatabaseTestBase class enables writing
and executing Unit Tests for business logic
based on ice.NET. The general challenge is to integrate unit testing with a technical
environment that is heavily database-related. This base class meets the challenge
and enables efficient, transparent unit testing.
Example
This example shows how to create an application-specific unit test
base class. The class defines the database name to be used for testing
("test_db01"), sets the path to the license file, ensures that the relevant
business objects assemblies ("PDTec.IceNet.Domain", "PDTec.ICR.BusinessLogic")
are loaded and that the SetUp and TearDown methods are connected with the unit
testing framework by code attributes.
public abstract class DatabaseTestBase : PDTec.IceNet.Sdk.Test.DatabaseTestBase
{
public DatabaseTestBase() : base("test_db01")
{
LicenseFilePath = @"..\..\..\..\license.xml";
BOAssemblyNames.Add("PDTec.IceNet.Domain");
BOAssemblyNames.Add("PDTec.ICR.BusinessLogic");
}
[SetUp]
protected override void SetUpBase()
{
base.SetUpBase();
}
[TearDown]
protected override void TearDownBase()
{
base.TearDownBase();
}
}
This example shows how to use the base class defined above to create
a test fixture. In the fixture-specific SetUp method the basic model and
data is imported. In the test case two business object methods
(IVehicle.CreateBooking, IBooking.CreateReceipt) are tested.
[TestFixture]
public class BookingsTest : DatabaseTestBase
{
[SetUp]
public void SetUp()
{
Repository.ExecuteTransaction(delegate
{
ImportModel(Assembly.Load("PDTec.ICR.BusinessLogic"), "PDTec.ICR.BusinessLogic.ModelXml.PDTec.ICR.icemodel");
ImportData(GetType().Assembly, "PDTec.ICR.Test.MasterData.ICR.Data.icedata");
});
}
[Test]
public void MakeBooking()
{
Repository.ExecuteTransaction(delegate
{
ICustomer pCustomer = Repository.GetFolderByKey(Constants.Folder.Customers).Get<ICustomer>(Repository.GetObjTypeByName("PDTec.ICR.Customer"), true, 1)[0];
IVehicle pVehicle = Repository.GetFolderByKey(Constants.Folder.Cars).Get<IVehicle>(Repository.GetObjTypeByName("PDTec.ICR.Vehicle"), true, 1)[0];
IBooking pBooking = pVehicle.CreateBooking(pCustomer, DateTime.UtcNow, DateTime.UtcNow.AddDays(2.0));
pBooking.CreateReceipt("System.Database");
});
Assert.That(Repository.RootFolder.GetChildFolders().Length, Is.GreaterThan(0));
Assert.That(ModelUtils.GetObjectsOfType(Repository, Repository.GetObjTypeByName("PDTec.ICR.Booking"), true, FindRange.Global, null, -1).Length, Is.EqualTo(1));
Assert.That(ModelUtils.GetObjectsOfType(Repository, Repository.GetObjTypeByName("Core.File"), true, FindRange.Global, null, -1).Length, Is.EqualTo(1));
}
}
Use the NUnit.exe application to run the test cases:
Testing Service Methods
This example shows how to call IceNetService implementations (here: MyService)
from unit tests directly, without generated code from the Service builder.
r0 = TestServiceAdapter<MyService>.Instance(this).MyMethod(p1, p2);