简体   繁体   English

具有工厂类单元测试的策略模式

[英]Strategy pattern with Factory class Unit testing

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. 我有一个策略模式类LeaveCalculator,并且基于请假类型,工厂类将实例化特定的计算器。
For the Unit Test part I am trying to verify the proper Leave type calculation is invoked when we call the LeaveCalculator calculate method. 对于单元测试部分,我试图验证当我们调用LeaveCalculator计算方法时是否调用了正确的请假类型计算。

I am using C# for mocking RhinoMocks. 我正在使用C#模拟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);
}

You have to redesign your code to use Dependency Injection . 您必须重新设计代码以使用依赖注入 In your case define ILeaveFactoryCategory with GetCategory method. 在您的情况下,请使用GetCategory方法定义ILeaveFactoryCategory。 Make your ProRate class dependent on it (for example set factory by constructor parameter). 使您的ProRate类依赖于它(例如,通过构造函数参数设置工厂)。 Then mock the factory interface not calculator itself and set expectations for them. 然后模拟工厂界面而不是计算器本身,并为它们设置期望。 Use mocked object as parameter for class under test (LeaveCalculator). 将模拟对象用作被测类(LeaveCalculator)的参数。 Verify expectations for you mocked object. 验证对模拟对象的期望。

ILeaveCalculation expectedCalculator = new MyCalculator();
LeaveCalculationType expectedCalculationType = LeaveCalculationType.MyType;

ILeaveFactoryCategory factoryMock = MockRepository.GenerateMock<ILeaveFactoryCategory >();

factoryMock.Expect(f => f.GetCategory(Arg<LeaveCalculationType>.Is.Equal(expectedCalculationType)).Returns(expectedCalculator);

var proRataClass = new ProRata(factoryMock);
var availableLeaveCount = proRataClass.Calculate(employee, systemDateTime.Now.Date, leaveCount);

factoryMock.VerifyAllExpectations();

This code verifies that factory was used not the result of calculation. 此代码验证未使用工厂而不是计算结果。 If you want to test results it's better to use Stub method instead of Expect and verify calculation result instead of expected behavior. 如果要测试结果,最好使用存根方法而不是Expect并验证计算结果而不是预期的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM