NHibernate Forge
The official new home for the NHibernate for .NET community

NHibernate Unit Testing

When using NHibernate we generally want to test only three things, that properties are persisted, that cascade works as expected and that queries return the correct result. In order to do all of those, we generally have to talk to a real database, trying to fake any of those at this level is futile and going to be very complicated.

We can either use a standard RDBMS or use an in memory database such as SQLite in order to get very speedy tests.

I have a pretty big implementation of a base class for unit testing NHibernate in Rhino Commons, but that has so many features that I forget how to use it sometimes. Most of those features, by the way, are now null & void because we have NH Prof, and can easily see what is going on without resorting to the SQL Profiler.

At any rate, here is a very simple implementation of that base class, which gives us the ability to execute NHibernate tests in memory.

public class InMemoryDatabaseTest : IDisposable
{
	private static Configuration Configuration;
	private static ISessionFactory SessionFactory;
	protected ISession session;

	public InMemoryDatabaseTest(Assembly assemblyContainingMapping)
	{
		if (Configuration == null)
		{
			Configuration = new Configuration()
				.SetProperty(Environment.ReleaseConnections,"on_close")
				.SetProperty(Environment.Dialect, typeof (SQLiteDialect).AssemblyQualifiedName)
				.SetProperty(Environment.ConnectionDriver, typeof(SQLite20Driver).AssemblyQualifiedName)
				.SetProperty(Environment.ConnectionString, "data source=:memory:")
				.SetProperty(Environment.ProxyFactoryFactoryClass, typeof (ProxyFactoryFactory).AssemblyQualifiedName)
				.AddAssembly(assemblyContainingMapping);

			SessionFactory = Configuration.BuildSessionFactory();
		}

		session = SessionFactory.OpenSession();

		new SchemaExport(Configuration).Execute(true, true, false, true, session.Connection, Console.Out);
	}

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

This just set up the in memory database, the mappings, and create a session which we can now use. Here is how we use this base class:

public class BlogTestFixture : InMemoryDatabaseTest
{
	public BlogTestFixture() : base(typeof(Blog).Assembly)
	{
	}

	[Fact]
	public void CanSaveAndLoadBlog()
	{
		object id;

		using (var tx = session.BeginTransaction())
		{
			id = session.Save(new Blog
			{
				AllowsComments = true,
				CreatedAt = new DateTime(2000,1,1),
				Subtitle = "Hello",
				Title = "World",
			});

			tx.Commit();
		}

		session.Clear();


		using (var tx = session.BeginTransaction())
		{
			var blog = session.Get<Blog>(id);

			Assert.Equal(new DateTime(2000, 1, 1), blog.CreatedAt);
			Assert.Equal("Hello", blog.Subtitle);
			Assert.Equal("World", blog.Title);
			Assert.True(blog.AllowsComments);

			tx.Commit();
		}
	}
}

Pretty simple, ah?


Posted abr 28 2009, 09:32 a.m. by Ayende
Filed under:

Comments

Fred Morrison wrote re: NHibernate Unit Testing
on 04-28-2009 11:56

Is there any type of free alternative to NH Prof?  After all, the whole point of using things like NHibernate is that they don't cost money, so it's quite naturual that we would look for a profiler that is also does not cost money.

Ayende wrote re: NHibernate Unit Testing
on 04-28-2009 16:28

Fred,

A few points.

a) No, there isn't. You can try use the log file, but the level of experience is on a totally different planet.

b) I fail to see the logic in saying that NHibernate's entire eco system should be free.

optimus wrote re: NHibernate Unit Testing
on 05-21-2009 15:06

"the whole point of using things like NHibernate is that they don't cost money".

Oh yeah! This is *CLEARLY*, *ENTIRELY* the *WHOLE* point.

The entire NH developer community would like to thank you for your incredible lack of tact.

Fabio Maulo wrote re: NHibernate Unit Testing
on 05-21-2009 19:34

@Fred

Sure there is!

The NHibernate log give you all you need... after that you should reorganize info and know what mean each thing you are seeing. This is completely free of charge.

If you want that somebody else do the same work for you and then give you some advise and waring about how you are using NH

you have various alternative starting from NH-Prof to consult some professional; as you can imagine this is not for free.

If you want all for free, please, do something with the supermarket where I'm going; they asking me money for each thing I'm buying.

Thanks.

work from home wrote work from home
on 08-16-2009 7:34

4. Refrain from connecting any computer to the Internet in any way. We understand the inconvenience that this may cause some Internet users, and we apologize. However, we are certain that any inconveniences will be more than made up for by the increased

Powered by Community Server (Commercial Edition), by Telligent Systems