Note that there are some explanatory texts on larger screens.

plurals
  1. POStrategy pattern with Factory class Unit testing
    primarykey
    data
    text
    <p>I was looking for some sample code for unit testing the strategy pattern method invocation. </p> <p>I have a strategy pattern class LeaveCalculator and based on the leave type the factory class will instantiate the specific calculator.<br> For the Unit Test part I am trying to verify the proper Leave type calculation is invoked when we call the LeaveCalculator calculate method.</p> <p>I am using C# for mocking RhinoMocks. </p> <p>Please let me know any code samples to do this?</p> <pre><code>public static class LeaveCategoryFactory { private static List&lt;ILeaveCalculation&gt; categories = new List&lt;ILeaveCalculation&gt;(); public static ILeaveCalculation GetCategory(LeaveCalculationType calculationType) { if (categories.Count == 0) { categories = Assembly.GetExecutingAssembly() .GetTypes() .Where(type =&gt; typeof(ILeaveCalculation).IsAssignableFrom(type) &amp;&amp; type.IsClass) .Select(type =&gt; Activator.CreateInstance(type)) .Cast&lt;ILeaveCalculation&gt;() .ToList(); } return categories.Where(x =&gt; 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&lt;ProRata&gt;(); var availableLeaveCount = proRataClass.Calculate(employee, systemDateTime.Now.Date, leaveCount); Assert.AreEqual(1, availableLeaveCount); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload