Geeks With Blogs
Lee Brandt's Blog You're only as smart as your last line of code

That’s right, I said it, I’ve been doing it, and I abso-effin-lutely love it.

A few months ago my co-worker, Troy, posted a question on Stack Overflow. We were doing integration testing on a project we were working on, and as the object graph grew larger, the code required to test query logic became painful. Since I am a sissy and don’t like pain, Troy asked the question on SO about how we might do it less painfully, and we got this answer, and it totally changed our TDD/BDD lives.

The answer came from Garry Shutler, here and I’ll just sort of re-iterate it.

We created a base Interface for our repositories IRepository<T>, with a method IQueryable<T> All(). like so:

   1:  public interface IRepository<T>
   2:  {
   3:      IQueryable<T> All();
   4:      // whatever else you want
   5:  }

 

Then we implement it like so:
   1:  public IQueryable<T> All()
   2:  {
   3:      return session.Linq<T>();
   4:  }

We can then mock the IRepository<T>:

   1:  _repository = MockRepository.GenerateMock<IRepository<Customer>>();

 

Then Stub it to return a predefined list of objects that we are searching
   1:  _repository.Stub(repo => repo.All()).Return(TestCustomers.All().AsQueryable());

 

Then we can test the login of a Customer search by:
   1:  [Test]
   2:  public void should_return_customers_matching_first_name_last_name_and_dob()
   3:  {
   4:      Assert.That(_returnedMatches.Contains(_firstNameLastNameDOBMatch));
   5:  }

 

 

 

 

 

Many thanks to Garry for all the time he saved us, I immediately subscribed to his blog and have not been disappointed. I hope this helps someone as much as it has helped us.

~L

Posted on Thursday, May 7, 2009 7:28 PM Dependency Injection , Learning | Back to top


Comments on this post: Using Linq to Test Your Query Logic Without Touching The Database

# re: Using NHibernate to Test Your Query Logic Without Touching The Database
Requesting Gravatar...
The only thing I would say is think about trying

The generics only need to be on the methods. That way you can reuse the same repository... pretty sweet code man!

public interface IRepository
{
IQueryable<T> All();
// whatever else you want
}
Left by Robz on May 07, 2009 9:47 PM

# re: Using NHibernate to Test Your Query Logic Without Touching The Database
Requesting Gravatar...
Thanks for the shout. Found you'd linked to me through Google Analytics (great tool by the way, get it set up if you haven't already).

Glad I could help you with your testing. You've actually made me realise a helper I have for this kind of test situation is blog worthy so keep an eye out for that one soon.

Had a look round your posts and they're good so you've got yourself a new subscriber!
Left by Garry Shutler on May 09, 2009 6:05 AM

# re: Using NHibernate to Test Your Query Logic Without Touching The Database
Requesting Gravatar...
The title of the post is misleading. You are using Linq to test queries without hitting the database, not nHibernate. Any orm with Linq support will allow this approach.
Left by Jon on Oct 01, 2009 1:36 AM

# re: Using Linq to Test Your Query Logic Without Touching The Database
Requesting Gravatar...
Excellent point Jon. Title changed.
Left by Lee Brandt on Oct 01, 2009 11:25 AM

Your comment:
 (will show your gravatar)


Copyright © Lee Brandt | Powered by: GeeksWithBlogs.net