Skip to content

Instantly share code, notes, and snippets.

  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save defeated/166781 to your computer and use it in GitHub Desktop.
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() {
CreateSessionSource();
this.session = SessionSource.CreateSession();
SessionSource.BuildSchema(true);
}
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