In my last post I talked about how I needed to eager load my data in order to send it back across the wire. Normally I am not a big fan of creating tests to ensure that I can hit the database, but in this case the act of eager loading needs to be verified and constant.
When I set out to create my tests, I was not exactly sure how I could verify this. The good news is there is a utility class called NHibernateUtil which has a method on it called .IsInitialized() which will return a bool. Below is the unit test I put together to ensure my data was always being eagerly loaded. Yes I know that this test does NOT take into account any new data which may need to be eagerly loaded, but it is at least a step in the right direction.
public void FetchDestinations_EnsureDataIsEagerLoaded()
var repository = new DestinationRepository();
IList destinations = repository.FetchDestinations();
// want to ensure that the data was eager loaded as we need/want
var destination = destinations.FirstOrDefault();
// incase there is no data in the DB
if ( destination != null )
Assert.That( NHibernateUtil.IsInitialized( destination.Endpoint ), Is.True );
Assert.That( NHibernateUtil.IsInitialized( destination.AssignedMessageTypeses ), Is.True );
As you can see from my test, I do need to add in come logic to ensure I am hitting my database and gathering data. I am normally not a fan of this, but I think it is needed here as I do not want to fail my test if there is no data. When this test runs I will always know that my data is being eagerly loaded which fulfills my needs.
Till next time,
03-06-2009 7:27 AM