views:

212

answers:

3

I am now ready to use NHibernate to persist my data layer access. I used DDD until that point and I use fake for unit tests and for the test site.

I know that I can use SchemaExport for unit/integration tests of my NHibernate concrete repositories but how should I generate the schema to be used for the test site ?

Should I create a special class in my tests that create the schema and insert the static data or should I generate the schema at the launch of the site if the database is not created ?

A: 

As a simple scenario you can miss use a unit test for that. Just create unit test called CreateSchema which will do the schemaexport. Then run it before you will run the other tests.

I have a similar question here on STO

Michal
My problem is not creating schema for the tests but for the production site.
Michaël Larouche
+2  A: 

Personally I like to use a small console application which loads all the mappings and executes a SchemaExport as below:

new SchemaExport(config).Execute(ddlScript => {
    using (var writer = new StreamWriter(fileName, true))
    {
        writer.Write(ddlScript);
        writer.Flush();
    }
}, false, false);

This console app runs as a step of the build script and the DDL script file then gets picked up by WiX and is included in the MSI package (which generates the whole database at install time).

Markus Dulghier
A: 

For your full build script I'd go with Markus's suggestion, for just running your unit tests I'd put

<property name="hbm2ddl.auto">create-drop</property>

in the app config of you test project - this will drop and recreate your schema everytime all the tests are run. Each unit test can add the data it needs to test.

Jon Spokes