tags:

views:

24

answers:

1

From NHibernate Unit Testing:

When testing NHibernate apps I usually test several things:

a) that I have correctly created the mapping

b) that I mapped all the persistent properties

c) that I have correctly defined cascades

And most importantly, that my queries return the correct results. That is the most common thing that I really want to test

What do each of these items mean, and how are they tested?

+1  A: 

I'll try and give you the short answer, especially considering that you already have Ayende's excellent posting to refer to. He doesn't actually show the code for a Blog object, the database equivalent of a Blog table, or the mapping in that post, but they all need to exist for his example to work

a) For the mapping to be correct means that NHib knows how to translate the Blog object to the Blog table and back again. Because his unit test persists a Blog entry and then loads it from the database, you can assume that the mapping for all of the properties being persisted and loaded must be mapped correctly.

b) This one is really addressing the 'completeness' of the mapping. In his example, he uses the Title property of the blog object, so that and any other properties that need to be persisted are part of his unit test. Presumably his unit test includes the complete set of Blog properties that need to be persisted.

c) This refers to the behavior of associated table cascades. For instance, if you want and Blog.Comments to be persisted automatically in the Comments db table when a Blog entry is saved, there should be a unit test for it, perhaps proving that you can retrieve Comments after a Blog save (Ayende doesn't show such a test in this post).

d) Ayende doesn't show a query test in this posting, but if he had a use case to retrieve blogs created over a specified date range he might have a test with blog entries over a week's period and then prove he can select only the ones created on some specified day.

This may seem like a relatively complicated topic (and it is!) but the only way to take the mystique out of it is to get your hands dirty and do it.

HTH,
Berryl

Berryl