Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
using NHibernate;
using FluentNHibernate.Conventions.Helpers;
using FluentNHibernate.Cfg.Db;
using FluentNHibernate.Testing;
namespace your.namespace.Tests {
/// <summary>
/// Base class for integration tests.
/// Sets up a fast, temporary instance of SQLite running in-memory for use in persistence tests.
/// </summary>
public class FluentNhibernateInMemoryDatabaseFixture {
protected static SingleConnectionSessionSourceForSQLiteInMemoryTesting SessionSource;
protected ISession session;
/// <summary>
/// Initializes a new instance of the <see cref="FluentNhibernateInMemoryDatabaseFixture" /> class.
/// Creates the single session and builds the database schema.
/// </summary>
public FluentNhibernateInMemoryDatabaseFixture() {
this.session = SessionSource.CreateSession();
private static void CreateSessionSource() {
if(SessionSource != null) return;
var config = SQLiteConfiguration.Standard.InMemory().ShowSql().ToProperties();
var model = new DomainPersistenceModel() // a subclass of FNH AutoPersistenceModel with my project's conventions
.ConventionDiscovery.Add(DefaultLazy.AlwaysFalse()); // no proxy classes for specification tests
SessionSource = new SingleConnectionSessionSourceForSQLiteInMemoryTesting(config, model);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment