One of my test fixtures does a query for every single mapped class. If one of the mapping has an error, the test fails. It is not fool-proof, but it has helped us so far. The second test below checks that all mapped classes are serializable. Adapt the code below to you own needs:
Code:
using NHibernate;
using NUnit.Framework;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace PatientSys.Data
{
[TestFixture]
public class TestMappingFixture : FixtureBase
{
[Test]
public void LoadAllMappedClasses()
{
Type[] typesToSkip = new Type[] {
typeof(PatientSys.Core.Domain.MissingDataRecord),
typeof(PatientSys.Core.Domain.CaseProgram.ExtendedInformation),
};
Dictionary<Type, Exception> typesWithError = new Dictionary<Type,Exception>();
IDictionary allClasses = this.NhSession.SessionFactory.GetAllClassMetadata();
foreach (Type mappedClassType in allClasses.Keys)
{
try
{
// Skip a few classes because they do something funky and do
// not actually map to a table.
bool skip = false;
foreach (Type typeToSkip in typesToSkip)
{
if (mappedClassType.Equals(typeToSkip))
{
skip = true;
break;
}
}
if (skip) continue;
// TODO: Fix the Note class hierarchy
//if (typeof(PatientSys.Core.Domain.Note).IsAssignableFrom(mappedClassType)) continue;
NhSession.Clear();
ICriteria crit = NhSession.CreateCriteria(mappedClassType)
.SetMaxResults(10);
IList list = crit.List();
}
catch (Exception ex)
{
typesWithError.Add(mappedClassType, ex);
}
}
if (typesWithError.Count > 0)
{
StringBuilder sb = new StringBuilder("The following mapped types have mapping error:\n");
foreach (KeyValuePair<Type, Exception> kvPair in typesWithError)
{
string errLine = string.Format("\t{0} -- {1}", kvPair.Key.FullName, kvPair.Value.Message);
sb.AppendLine(errLine);
}
Assert.Fail(sb.ToString());
}
}
[Test]
public void CheckAllMappedClassesAreSerializable()
{
List<Type> typesWithError = new List<Type>();
IDictionary allClasses = this.NhSession.SessionFactory.GetAllClassMetadata();
foreach (Type mappedClassType in allClasses.Keys)
{
if ((int)(mappedClassType.Attributes & System.Reflection.TypeAttributes.Serializable) == 0)
{
typesWithError.Add(mappedClassType);
}
}
if (typesWithError.Count > 0)
{
StringBuilder sb = new StringBuilder("The following mapped types are not serializable:\n");
foreach (Type type in typesWithError)
{
string errLine = string.Format("\t{0}", type.FullName);
sb.AppendLine(errLine);
}
Assert.Fail(sb.ToString());
}
}
}
}