Created
March 2, 2013 20:16
-
-
Save jasonmitchell/5073089 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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