Devlico.Us
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @devlicious

Christopher Bennage

Our WPF book is now available!


TDD Example: Querying a Repository

I'm always a little intimidated when I post code examples.  There are a lot of smart (and opinionated) people out there reading blogs.  Fear won't get us anywhere though.  That said, I'm posting this example and asking for criticism.

Here's the story.  A school of music needs to schedule auditions for incoming students. The faculty need to be able to view the schedule for the next two upcoming terms, and some of staff needs to be able to edit the audition schedule.

The "schedule" is really a set of rooms set aside on certain dates for certain types of students.  For example, room 1874 will be used for violins on September 21. Thus, a ScheduledRoom is a certain on a certain date, with rules about who can audition there.  Right now, we're just concerned with retrieving the list of rooms for a given Term, which is a unique combination of year and semester.

We have a repository for handling audition schedules, and a service that talks to the repository.  The service is in turn accessed by a desktop client, and a web client.  I'm going to focus on just the repository here.

namespace Specifications.Integrated.The_repository_for
{
    [TestFixture]
    public class the_audition_schedule
    {
        private AuditionScheduleRepository _repository;

        [SetUp]
        public void SetUp
        {
            _repository = new AuditionScheduleRepository();
        }

        [Test, RollBack]
        public void can_retrieve_the_list_of_scheduled_rooms_for_a_given_term()
        {
            //setup data for test
            Term fall2000 = a_term_for_Fall_2000();
            Term spring2001 = a_term_for_Spring_2001();

            schedule_a_room_for_(fall2000);
            schedule_a_room_for_(fall2000);
            schedule_a_room_for_(fall2000);
            schedule_a_room_for_(spring2001);

            //here's the actual code under test
            IList<ScheduledRoom> rooms = _repository.GetRooms(fall2000);

            //verify the results
            Assert.AreEqual(3, rooms.Count);
        }
    }
}

The methods a_term_for_Fall_2000(), a_term_for_Spring_2001(), and schedule_a_room_for_() are helper methods that do nothing more than persist some test data.  In my [Setup], I create a new instance of _repository, so that it's fresh for every test.

What am I really testing here?  An important question, Joey Beninghove has a good post on the topic here.

I want to ensure that GetRooms() behaves the way I expect.  I expect it to return 3 out of the 4 terms I have persisted. The actual implementation for GetRooms() builds an NHibernate.ICriteria and call its List<>() method.

Thoughts? Comments?  More examples to come...

kick it on DotNetKicks.com


Comments

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# August 27, 2007 9:29 PM

Sean Chambers said:

Your code snippet there is displaying all the <br /> tags and makes it very difficult to read the code.

# August 28, 2007 7:15 AM

Sean Chambers said:

looks pretty straight forward to me.

I personally use SqlCE and Nhibernate when performing Repository tests. This way the insertion actually occurs to a throw away database file. I found using embedded databases are much easier than resetting database state on every test.

I have an article on thecodeproject named "TDD and SqlCE".

Good luck!

# August 28, 2007 7:27 AM

Christopher Bennage said:

@Sean - thanks, I've removed the <br/> tags twice now.

I'm not sure what I did wrong.  :-P

# August 28, 2007 8:46 AM

The Daily Grind 1217 at aoortic! dot com said:

Pingback from  The Daily Grind 1217 at  aoortic! dot com

# September 18, 2007 10:09 PM

About Christopher Bennage

Christopher is a software developer and consultant at Blue Spire Consulting, a company he co-founded with Rob Eisenberg in 2006. He is a Christian, a marginal musician, and an armchair philosopher. His interests include programming, liberal education, science, truth, beauty, and a number of deceased British authors (C. S. Lewis, G. K. Chesterton, and most recently Owen Barfield.) He lives in Tallahassee, FL with his wife and three children and still prefers to play as the Night Elves in WarCraft 3. Check out Devlicio.us!

Our Sponsors

Red-Gate!