Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use mocking for the following example

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

like image 547
czuroski Avatar asked Sep 15 '10 13:09

czuroski


3 Answers

First, don't do this:

DateTime date = new DateTime();
date = DateTime.Parse("2010-08-14");

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, and ReturnsCorrectNextSaturdayWhenCrossesEndOfYear.

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.

like image 51
Philip Rieck Avatar answered Nov 14 '22 14:11

Philip Rieck


Mocking is used to satisfy dependencies.

For instance. Consider if you have a class that loads users from a database using a IDataLayer (wrapper around the database)

public class UserService
{
    public UserService(IDataLayer layer) {}
    public User GetById(int id)
} 

When testing, you don't want to test against a database. It makes it hard to provide data and check the result. Instead, you mock a IDataLayer object to be able to manually provide a user to the UserService. It makes it a whole lot easier to validate that the UserService does what it's supposed to do.

As for your test method. I would break it up into two methods, since you are running two different tests (on the same method though)

like image 36
jgauffin Avatar answered Nov 14 '22 13:11

jgauffin


In this case a mocking framework is not necessary and thus shouldn't be used.

Your test is fairly reasonable. I would personally inline most of the date parsing for better readability:

[TestMethod()]
    public void NextSaturdayTest()
    {
        DateTime actual = DateExtensions.NextSaturday(DateTime.Parse("2010-08-14"));
        Assert.AreEqual(DateTime.Parse("2010-08-21"), actual);

        actual = DateExtensions.NextSaturday(DateTime.Parse("2010-08-19"));
        Assert.AreEqual(DateTime.Parse("2010-08-21"), actual);
    }
like image 34
Grzenio Avatar answered Nov 14 '22 13:11

Grzenio