Click here to Skip to main content
15,886,519 members
Articles / All Topics

Unit Testing with an InMemory Database and NBuilder

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
9 Sep 2011CPOL1 min read 11.3K   1  
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.

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

Permalink | Leave a comment  »

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead Independent Consultant
United States United States
___________________________
J A M E S C O L E M A N
Director, Technical Services
Linked-In: http://www.linkedin.com/in/jameswcoleman
Blog: ledtalks.wordpress.com

Comments and Discussions

 
-- There are no messages in this forum --