Thursday, September 8, 2011

Unit Testing with an InMemory Database and NBuilder

I am throwing a lot of cycles to my Ergo project lately. I will go into more detail of the Ergo project later when it starts to take more shape and get some legs but one thing it is allowing me to do is get back to a true TDD project. A couple of cool things I have introduced to my unit tests are NBuilder and leveraging Fluent NHibernate and SQLite InMemory to make testing the NHibernate layer a bit easier.

When using NHibernate you generally only need to test 3 things:

  • The Properties are being persisted
  • Cascading works as expected
  • Queries return the correct results

By using an in memory SQLite database we can do this testing while keeping our unit tests nice and speedy. Below is my InMemoryDatabaseTest class that my unit tests can inherit from to test our NHibernate layer.

Screen1

Now that we have our InMemoryDatabaseTest class setup, we can structure our unit tests like below. One nifty class I found today that you will see in the screenshot is the Builder class. This is from http://nbuilder.org/ and http://code.google.com/p/nbuilder/. So instead of having to create a new Page instance and populate it with a bunch of dummy data I can just use 1 line of code to new up a Page which I can then persist to the InMemory database and then use it to Assert against when I query.

Screen2

No comments:

Post a Comment