public class ExpenseReport
{
private readonly DateTime _weekEndingDate;
public event ErrorEventHandler InvalidWeekEndingDate;
public delegate void ErrorEventHandler();
public ExpenseReport(DateTime weekEndingDate)
{
_weekEndingDate = weekEndingDate;
}
public DateTime WeekEndingDate
{
get { return _weekEndingDate; }
}
public void Validate()
{
if (_weekEndingDate < DateTime.Today)
{
InvalidWeekEndingDate();
}
}
}
Testing this class becomes easy and clean with NUnit and anonymous methods:
[TestFixture]
public class ExpenseReportTests
{
[Test]
public void ValidExpenseReport()
{
ExpenseReport report = new ExpenseReport(DateTime.Today.AddDays(2));
report.InvalidWeekEndingDate += delegate { Assert.Fail(); };
report.Validate();
}
[Test]
public void InvalidWeekEndingDateReport()
{
bool methodCalled = false;
ExpenseReport report = new ExpenseReport(DateTime.Today.AddDays(-2));
report.InvalidWeekEndingDate += delegate { methodCalled = true; };
report.Validate();
Assert.IsTrue(methodCalled);
}
}
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.