Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds

Using the Fluent NHibernate Persistence Tester and the Ghostbusters Test

Save for later
  • 3 min read
  • 06 Oct 2010

article-image

NHibernate 3.0 Cookbook


using-fluent-nhibernate-persistence-tester-and-ghostbusters-test-img-0

Get solutions to common NHibernate problems to develop high-quality performance-critical data access applications

  • Master the full range of NHibernate features
  • Reduce hours of application development time and get better application architecture and performance
  • Create, maintain, and update your database structure automatically with the help of NHibernate
  • Written and tested for NHibernate 3.0 with input from the development team distilled in to easily accessible concepts and examples
  • Part of Packt's Cookbook series: each recipe is a carefully organized sequence of instructions to complete the task as efficiently as possible


     The reader would benefit from reading the previous article on Testing Using NHibernate Profiler and SQLite.

Using the Fluent NHibernate Persistence Tester


Mappings are a critical part of any NHibernate application. In this recipe, I'll show you how to test those mappings using Fluent NHibernate's Persistence tester.

Getting ready


Complete the Fast testing with SQLite in-Memory database recipe mentioned in the previous article.

How to do it...

  1. Add a reference to FluentNHibernate.
  2. In PersistenceTests.cs, add the following using statement:

    using FluentNHibernate.Testing;


    
    

  3. Add the following three tests to the PersistenceTests fixture:

    [Test]

    Unlock access to the largest independent learning library in Tech for FREE!
    Get unlimited access to 7500+ expert-authored eBooks and video courses covering every tech area you can think of.
    Renews at €14.99/month. Cancel anytime


    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();
    }

    
    

  4. Run these tests with NUnit.

How it works...


The Persistence tester in Fluent NHibernate can be used with any mapping method. It performs the following four steps:

  1. Create a new instance of the entity (Product, ActorRole, Movie) using the values provided.
  2. Save the entity to the database.
  3. Get the entity from the database.
  4. Verify that the fetched instance matches the original.


At a minimum, each entity type should have a simple Persistence test, such as the one shown previously. More information about the Fluent NHibernate Persistence tester can be found on their wiki at http://wiki.fluentnhibernate.org/Persistence_specification_testing

See also

  • Testing with the SQLite in-memory database
  • Using the Ghostbusters test