3

使用 TDD 开发事件调度程序并为以下课程编写测试项目。

决定写一个构造函数逻辑的测试方法

public class TechDay
{
    public Session MorningSlot { get; set; }
    public Session EveningSlot { get; set; }

    public TechDay()
    {
        this.MorningSlot = new Slot();
        this.EveningSlot = new Slot();

        this.MorningSlot.Sessions= new List<Session>();
        this.EveningSlot.Sessions= new List<Session>();
        this.ConfigureEventSettings();
    }

    protected virtual void ConfigureEventSettings()
    { 
      CultureInfo provider = CultureInfo.InvariantCulture;
      this.MorningSlot.StartTime = DateTime.ParseExact("9:00 AM", "h:mm tt", provider);
      this.MorningSlot.EndTime = DateTime.ParseExact("12:00 PM", "h:mm tt", provider);
      this.EveningSlot.StartTime = DateTime.ParseExact("1:00 PM", "h:mm tt", provider);
      this.EveningSlot.EndTime = DateTime.ParseExact("5:00 PM", "h:mm tt", provider);
    }
}

测试方法

[TestMethod]
public void CheckMorningSlot()
{
    TechDay techday=new TechDay();
    Assert.IsNotNull(techday.MorningSlot);
}

[TestMethod]
public void CheckEveningSlot()
{
    TechDay techday=new TechDay();
    Assert.IsNotNull(techday.EveningSlot);
}

[TestMethod]
public void CheckEveningSlotSessions()
{
    TechDay techday=new TechDay();
    Assert.IsNotNull(techday.EveningSlot.Sessions);
}

[TestMethod]
public void CheckMorningSlotSessions()
{
    TechDay techday=new TechDay();
    Assert.IsNotNull(techday.MorningSlot.Sessions);
}

我是否需要编写不同的方法来检查构造函数中的不同参数初始化?也不是构造函数调用另一个方法。

为这段代码编写测试方法的最佳方法是什么?

4

2 回答 2

2

您应该测试代码的功能需求,而不是每一位代码。那么您正在测试的功能是什么?如果要求早上 9 点开始,那么您的测试将类似于:

[TestMethod]
public void Morning_slot_starts_at_nine_am()
{
    var expected = DateTime.ParseExact("9:00 AM", "h:mm tt", CultureInfo.InvariantCulture);
    var techDay = new TechDay();
    var actual = techDay.MorningSlot.StartTime;
    Assert.AreEqual(expected, actual);
}
于 2013-04-02T09:02:50.503 回答
1

您必须将配置逻辑提取到另一个类。使用接口来模拟(参见Moq)。你会得到简单的测试。

public class TechDay
{
    public Session MorningSlot { get; set; }
    public Session EveningSlot { get; set; }

    public TechDay(IEventConfigurator morningConfigurator, IEventConfigurator eveningConfigurator)
    {
        MorningSlot = new Session();
        morningConfigurator.Configure(MorningSlot);

        EveningSlot = new Session();
        eveningConfigurator.Configure(EveningSlot);
    }
}

public interface IEventConfigurator
{
    void Configure(Session session);
}

public class Session
{
    public static DateTime StartTime { get; set; }
    public static DateTime EndTime { get; set; }
}

public class FromStringEventConfigurator : IEventConfigurator
{
    private readonly string _begin;
    private readonly string _end;

    public FromStringEventConfigurator(string begin, string end)
    {
        _begin = begin;
        _end = end;
    }

    public void Configure(Session session)
    {
        CultureInfo provider = CultureInfo.InvariantCulture;
        Session.StartTime = DateTime.ParseExact(_begin, "h:mm tt", provider);
        Session.EndTime = DateTime.ParseExact(_end, "h:mm tt", provider);
        // ...
    }
}
于 2013-04-02T09:31:16.460 回答