Click here to Skip to main content
13,050,429 members (132,308 online)
Click here to Skip to main content
Add your own
alternative version


6 bookmarked
Posted 28 Aug 2011

Unit Testing FluentNHibernate...

, 28 Aug 2011
Rate this:
Please Sign up or sign in to vote.
Unit testing code for Fluent NHibernate

As I recently posted, I just got back to unit testing... I thought of a small personal project to start practicing on, and got to work. My project consisted of using Fluent NHibernate, so I wanted to create unit tests to make sure my mappings were good.

I came across an old blog post of Ayende explaining that he created a base class that exports all your mappings into an in-memory SQLite database just for testing. This concept seemed really good to me, so I tried it myself, but ran into some small problems while trying to configure it for Fluent NHibernate, so I will post the new version to Ayende's class that works for FNH:

In case you don't have the SQLite providers yet, you can download them here.

This is the class:

public class InMemoryDatabaseTest : IDisposable
    private static Configuration configuration;
    private static ISessionFactory SessionFactory;
    protected ISession session { get; set; }

    public InMemoryDatabaseTest(Assembly assemblyContainingMapping)
        SessionFactory = Fluently.Configure()
            .Mappings(m => m.FluentMappings
            .ExposeConfiguration(x => configuration = x)

        session = SessionFactory.OpenSession();

        SchemaExport export = new SchemaExport(configuration);
        export.Execute(true, true, false, session.Connection, null);

    public void Dispose()

And then, all you have to do to test your mapping would be something like this:

public class UserTests : InMemoryDatabaseTest
    public UserTests() : base(typeof(UserMapping).Assembly)
    { }

    public void UserMapping_CanSaveAndLoadUser()
        object id;

        using (var tx = session.BeginTransaction())
            id = session.Save(new Dal.Entities.User()
                Username = "unittest",
                Password = "unittest1234",
                Email = ""



        using (var tx = session.BeginTransaction())
            var user = session.Get<Dal.Entities.User>(id);

            Assert.AreEqual(user.Username, "unittest");
            Assert.AreEqual(user.Password, "unittest1234");
            Assert.AreEqual(user.Email, "");


It's that easy! :) Thanks Ayende!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Gilly Barr
Web Developer
Israel Israel
Started programming e-commerce sites with PHP & MySQL at the age of 14. Worked for me well for about 5 years.

Transfered to C# &, while serving in the IDF.
Worked on the 'Core Performance' Team at (Sears Israel)
Currently working at

Check out my blog!
or my twitter

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170713.1 | Last Updated 28 Aug 2011
Article Copyright 2011 by Gilly Barr
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid