Created
September 9, 2012 17:06
-
-
Save davybrion/3685715 to your computer and use it in GitHub Desktop.
code snippets for "Unit Testing An NHibernate Application" post
This file contains hidden or 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
public class NHibernateTest | |
{ | |
private Configuration configuration; | |
protected ISessionFactory sessionFactory; | |
[TestFixtureSetUp] | |
public void TestFixtureSetup() | |
{ | |
configuration = new Configuration() | |
.Configure() | |
.AddAssembly("YourAssemblyName"); | |
new SchemaExport(configuration).Create(false, true); | |
sessionFactory = configuration.BuildSessionFactory(); | |
} | |
[TestFixtureTearDown] | |
public void TestFixtureTearDown() | |
{ | |
new SchemaExport(configuration).Drop(false, true); | |
} | |
protected ISession CreateTransactionalSession() | |
{ | |
var session = sessionFactory.OpenSession(); | |
session.BeginTransaction(); | |
return session; | |
} | |
} |
This file contains hidden or 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
public abstract class NHibernateTest | |
{ | |
protected static readonly ActiveSessionManager activeSessionManager = new ActiveSessionManager(); | |
private UnitOfWork unitOfWork; | |
protected ISession session; | |
protected abstract ISessionProvider GetSessionProvider(); | |
protected virtual IActiveSessionManager GetActiveSessionManager() | |
{ | |
return activeSessionManager; | |
} | |
[SetUp] | |
public void SetUp() | |
{ | |
BeforeSetup(); | |
var sessionManager = GetActiveSessionManager(); | |
unitOfWork = new UnitOfWork(GetSessionProvider(), sessionManager); | |
session = sessionManager.GetActiveSession(); | |
unitOfWork.CreateTransaction(); | |
AfterSetup(); | |
} | |
[TearDown] | |
public void TearDown() | |
{ | |
BeforeTearDown(); | |
if (unitOfWork != null) | |
{ | |
unitOfWork.Dispose(); | |
} | |
unitOfWork = null; | |
AfterTearDown(); | |
} | |
protected virtual void BeforeSetup() { } | |
protected virtual void AfterSetup() { } | |
protected virtual void BeforeTearDown() { } | |
protected virtual void AfterTearDown() { } | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment