I was looking for some sample code for unit testing the strategy pattern method invocation.
I have a strategy pattern class LeaveCalculator and based on the leave type the factory class will instantiate the specific calculator.
For the Unit Test part I am trying to verify the proper Leave type calculation is invoked when we call the LeaveCalculator calculate method.
I am using C# for mocking RhinoMocks.
Please let me know any code samples to do this?
public static class LeaveCategoryFactory
{
private static List<ILeaveCalculation> categories = new List<ILeaveCalculation>();
public static ILeaveCalculation GetCategory(LeaveCalculationType calculationType)
{
if (categories.Count == 0)
{
categories = Assembly.GetExecutingAssembly()
.GetTypes()
.Where(type => typeof(ILeaveCalculation).IsAssignableFrom(type) && type.IsClass)
.Select(type => Activator.CreateInstance(type))
.Cast<ILeaveCalculation>()
.ToList();
}
return categories.Where(x => x.CalculationType == calculationType).FirstOrDefault() as ILeaveCalculation;
}
}
[TestMethod]
public void ShouldReturnOneWhenAvailableLeaveCountIs12AndWorkedForAMonth()
{
leaveCount.StartDate = systemDateTime.Now.Date.AddMonths(-1);
leaveCount.EndDate = systemDateTime.Now.Date.AddMonths(11);
leaveCount.Count = 12;
var proRataClass = MockRepository.GenerateMock<ProRata>();
var availableLeaveCount = proRataClass.Calculate(employee, systemDateTime.Now.Date, leaveCount);
Assert.AreEqual(1, availableLeaveCount);
}