While trying to get up to speed on using Castle’s ActiveRecord framework, one of the first things I needed to do was figure out how to test my ActiveRecord objects. There are some a good articles that describes how to us a base test class to get your unit tests up and running.
In a nutshell you need to have a TestFixtureStartup to initialize ActiveRecord. You need to call the ActiveRecordStarter.Initialize to get AR ready to go.
However, I have yet to find to opposite of this method to stop ActiveRecord on TestFixtureTearDown. This becomes a problem when you have multiple test classes. AR will yell at for already creating an instance. I solved this with a static guard variable.
private static bool _isActivated = false;
…
if(!_isActivated)
{
_isActivated = true;
ActiveRecordStarter.Initialize(source, typeof(…));
}
If there is a better way of doing this please let me know.
One very cool thing is that you can leverage Nhibernate’s ability to create and drop schemas. ActiveRecordStarter.CreateSchema() will create your database schema based off of your ActiveRecord classes. I like this feature, though I wouldn’t use it for real integration tests because I want to make sure that my database migration process works when I deploy to production.