views:

119

answers:

1

I'm running some unit tests using Castle ActiveRecord that interact with a database. I have a procedure to drop the database (if it exists), then re-create it, before I interact with it in each test.

If I run one test, this works fine.

If I run multiple tests, the second one fails because it can't drop the database.

Is there some way in Castle ActiveRecord to tell it to shut down and let go of the database?

+2  A: 

Instead of dropping the whole database, I recommend dropping and recreating the schema.

To drop the schema: ActiveRecordStarter.DropSchema();

To create the schema: ActiveRecordStarter.CreateSchema();

To reinitialize ActiveRecord: ActiveRecordStarter.ResetInitializationFlag(); then reconfigure it.

See the base AR test class for guidance.

For testing, I recommend taking a look at the new InMemoryTest.

See also: docs for ActiveRecord unit-testing.

Mauricio Scheffer
That appears to work perfectly. I changed my routine to only create the database if it doesn't exist, and to call DropSchema before CreateSchema. Now my tests run in sequence just fine.
Kyralessa
Also, where's that base test class? I can't find it in the 2.0 download; has it been added since then? (I guess I could always add my own version of it.) And what would I use the InMemoryTest for?
Kyralessa
That base test class is for the internal ActiveRecord tests, you won't see it in the library itself but you can just copy it.
Mauricio Scheffer
InMemoryTest uses an in-memory sqlite database for fast testing. See http://mortslikeus.blogspot.com/2009/07/activerecord-beta-1-released.html for details.
Mauricio Scheffer