Saturday, August 27, 2011

Unit testing FluentNHibernate...

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 db 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!

No comments:

Post a Comment