Click here to Skip to main content
15,884,388 members
Articles / Programming Languages / C#

Unit Testing FluentNHibernate...

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
28 Aug 2011CPOL 21.9K   6  
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:

C#
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:

C#
[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! :) Thanks Ayende!

License

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


Written By
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.
Worked on the 'Core Performance' Team at ShopYourWay.com (Sears Israel)
Currently working at Logz.io

Check out my blog!
or my twitter

Comments and Discussions

 
-- There are no messages in this forum --