Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Unit testing FluentNHibernate...

, 28 Aug 2011 CPOL
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()
            .Database(SQLiteConfiguration.Standard.InMemory().ShowSql())
            .ProxyFactoryFactory(typeof(ProxyFactoryFactory))
            .Mappings(m => m.FluentMappings
                            .Add(typeof(UserMapping))
                      )
            .ExposeConfiguration(x => configuration = x)
            .BuildSessionFactory();

        session = SessionFactory.OpenSession();

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

    public void Dispose()
    {
        session.Dispose();
    }
}

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

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

    [TestMethod]
    public void UserMapping_CanSaveAndLoadUser()
    {
        object id;

        using (var tx = session.BeginTransaction())
        {
            id = session.Save(new Dal.Entities.User()
            {
                Username = "unittest",
                Password = "unittest1234",
                Email = "unittest@gmail.com"
            });

            tx.Commit();
        }

        session.Clear();

        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, "unittest@gmail.com");

            tx.Commit();
        }
    }
}

It's that easy! Smile | :) Thanks Ayende!

License

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

Share

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# & asp.net, while serving in the IDF.
 
Currently working as a web developer for Sears Israel (SHC).
 
Check out my blog!

Comments and Discussions

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