I am relatively new to using TDD and have been reading about mocking objects lately. I have the following test to test a method that given a date returns the next saturday.
[TestMethod()]
public void NextSaturdayTest()
{
DateTime date = new DateTime();
date = DateTime.Parse("2010-08-14");
DateTime expected = new DateTime();
expected = DateTime.Parse("2010-08-21");
DateTime actual;
actual = DateExtensions.NextSaturday(date);
Assert.AreEqual(expected, actual);
date = DateTime.Parse("2010-08-19");
expected = DateTime.Parse("2010-08-21");
actual = DateExtensions.NextSaturday(date);
Assert.AreEqual(expected, actual);
}
first off, does this represent good testing practices?
Second, what is the advantage of utilizing a mock framework to create this test?
Let me know if I can offer any more information.
Thanks for any thoughts
First, don’t do this:
You are creating a new datetime, then throwing it away when you parse a string to get a new datetime.Remember, test code should still be good code.
Second, a good test tests one thing. You may have multiple tests like
ReturnsCorrectNextSaturdayGivenAWednesday,ReturnsCorrectNextSaturdayWhenCrossesEndOfMonth, andReturnsCorrectNextSaturdayWhenCrossesEndOfYear.And finally, there’s no reason to mock here. A mock would be appropriate if your DateExtensions called into another component (say a database), and you wanted to fake that call. So instead of testing DateExtensions + Data Access, you’d only be testing the DateExtensions and when it called the data access layer, it would be a mock that your test set up.