Reputation: 5266
Working on a event scheduler with TDD and writing test project for the below class.
Decided to write a test methods for Constructor logic
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);
}
}
Test Methods
[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);
}
Do I need to write different methods to check different parameter initialization in a constructor? Also not that Constructor calls another method.
What is the best way of writing the test methods for this code?
Upvotes: 3
Views: 201
Reputation: 1750
You must extract configuration logic to another class. Use interfaces to mock (see Moq). And you will get simple tests.
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);
// ...
}
}
Upvotes: 1
Reputation: 3002
You should be testing the functional requirements of your code rather than each bit of code. So what is the functionality that you are testing? If there is a requirement that the morning slot starts at 9am then your test would be something like:
[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);
}
Upvotes: 2