【发布时间】:2013-04-02 06:30:31
【问题描述】:
使用 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);
}
我是否需要编写不同的方法来检查构造函数中的不同参数初始化?也不是构造函数调用另一个方法。
为这段代码编写测试方法的最佳方式是什么?
【问题讨论】:
-
构造函数中的虚拟方法调用不是个好主意。
-
@Tilak,我只是将该方法设置为虚拟方法,因为其他 conf 事件也可以继承 TechDay 并更改开始和结束时间。例如:科技之夜。什么是最好的建议?
-
Billa,如果你能正确地回忆起构造函数内部的基构造函数、派生构造函数和虚方法之间的调用顺序,以及每次调用时将/不会初始化哪些字段,那可能就可以了。
-
@AlexeiLevenkov,我把它改成了
private void并删除了virtual
标签: c# unit-testing tdd test-project