Mappings are a critical part of any NHibernate application. In this recipe, we'll show you how to use Fluent NHibernate's persistence testing, in order to make sure that your mappings work as expected. You don't have to use Fluent NHibernate mappings in order to take advantage of this recipe.
Complete the Fast testing with the SQLite in-Memory database recipe mentioned earlier in this chapter.
FluentNHibernate
using the NuGet Package Manager Console by executing the following command:Install-Package FluentNHibernate
PersistenceTests.cs
, add the following using
statement:using FluentNHibernate.Testing;
PersistenceTests
fixture:[Test] public void Product_persistence_test() { new PersistenceSpecification<Product>(Session) .CheckProperty(p => p.Name, "Product Name") .CheckProperty(p => p.Description, "Product Description") .CheckProperty(p => p.UnitPrice, 300.85M) .VerifyTheMappings(); } [Test] public void ActorRole_persistence_test() { new PersistenceSpecification<ActorRole>(Session) .CheckProperty(p => p.Actor, "Actor Name") .CheckProperty(p => p.Role, "Role") .VerifyTheMappings(); } [Test] public void Movie_persistence_test() { new PersistenceSpecification<Movie>(Session) .CheckProperty(p => p.Name, "Movie Name") .CheckProperty(p => p.Description, "Movie Description") .CheckProperty(p => p.UnitPrice, 25M) .CheckProperty(p => p.Director, "Director Name") .CheckList(p => p.Actors, new List<ActorRole>() { new ActorRole() { Actor = "Actor Name", Role = "Role" } }) .VerifyTheMappings(); }
NUnit
.The Persistence tester in Fluent NHibernate can be used with any mapping method. It performs the following four steps:
Product
, ActorRole
, Movie
) using the values provided.At a minimum, each entity type should have a simple persistence test, such as the one shown previously. You can find more information about Fluent NHibernate persistence testing at https://github.com/jagregory/fluent-nhibernate/wiki/Persistence-specification-testing.
3.138.37.20