Skip to content

Instantly share code, notes, and snippets.

@jasonmitchell
Created March 2, 2013 20:16
Show Gist options
  • Save jasonmitchell/5073089 to your computer and use it in GitHub Desktop.
Save jasonmitchell/5073089 to your computer and use it in GitHub Desktop.
using NHibernate;
using NUnit.Framework;
namespace UnitTests
{
public abstract class AbstractInMemoryDataFixture
{
private ISession session;
[SetUp]
public void BaseSetup()
{
session = InMemorySessionFactoryProvider.Instance.OpenSession();
}
[TearDown]
public void BaseTearDown()
{
if (session != null)
session.Dispose();
}
protected ISession Session
{
get { return session; }
}
}
}
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
using MyWebProject;
namespace UnitTests
{
public class InMemorySessionFactoryProvider
{
private static InMemorySessionFactoryProvider instance;
public static InMemorySessionFactoryProvider Instance
{
get { return instance ?? (instance = new InMemorySessionFactoryProvider()); }
}
private ISessionFactory sessionFactory;
private Configuration configuration;
private InMemorySessionFactoryProvider() { }
public void Initialize()
{
sessionFactory = CreateSessionFactory();
}
private ISessionFactory CreateSessionFactory()
{
return Fluently.Configure()
.Database(SQLiteConfiguration.Standard.InMemory().ShowSql())
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<TypeInMyWebProject>())
.ExposeConfiguration(cfg => configuration = cfg)
.BuildSessionFactory();
}
public ISession OpenSession()
{
ISession session = sessionFactory.OpenSession();
var export = new SchemaExport(configuration);
export.Execute(true, true, false, session.Connection, null);
return session;
}
public void Dispose()
{
if(sessionFactory != null)
sessionFactory.Dispose();
sessionFactory = null;
configuration = null;
}
}
}
using NUnit.Framework;
namespace UnitTests
{
[SetUpFixture]
public class TestSetupFixture
{
[SetUp]
public void Setup()
{
InMemorySessionFactoryProvider.Instance.Initialize();
}
[TearDown]
public void TestTeardown()
{
InMemorySessionFactoryProvider.Instance.Dispose();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment