Tutorial :Testing nHibernate mappings



Question:

I have just started a new project using nHibernate and Fluent for mapping. The architect has sent me a database from which I have generated several hundred entity classes and the corresponding Fluent mapping files. I know this is not the ideal DDD way of doing things but life is rarely ideal.

What I want to do is test that all the mappings are correct, columns mapped correct, OneToMany, ManyToMany etc. Is there some automated or easy way to do this? I have considered just writing a simple repository that loads a record from every entity and make sure no exception is raised, but most of the tables have no data in them yet.


Solution:1

Have a look at the PersistenceSpecification in Fluent NHibernate. It's hardly perfect, but it handles a lot of simple cases well.


Solution:2

In order to test ORM mappings one strategy I've used that has saved hours of work involves using a row test approach in unit tests, like the RowTest attribute in MBUnit or NUnit. This will save you from writing individual unit tests for distinct row values. take a look at this webcast for a quick start.

Regarding the database you could follow 2 strategies: If you need to test in a specific DB instance or engine you can use transactions and make sure all writings to the DB are rolled back after the asserts. If you can use your own instance and engine you can use SQLLite or SQL CE as a unit test only DB. Since this DB will be only used in unit tests you can always create a brand new DB every time you run the unit tests.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »