Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
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

Posted 08-26-2007 11:07 PM by Christopher Bennage
Filed under: ,

[Advertisement]

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)