Click here to Skip to main content
Click here to Skip to main content

Unit Testing with an InMemory Database and NBuilder

By , 9 Sep 2011
Rate this:
Please Sign up or sign in to vote.

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)

About the Author

James Coleman
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
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 9 Sep 2011
Article Copyright 2011 by James Coleman
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid