Blog Stats
  • Posts - 62
  • Articles - 1
  • Comments - 35
  • Trackbacks - 76

 

NUnit Tracer, NHibernate NUnit base class and other lessons

I have finally gotten smart and started using NUnit to drive my coding. Man, I can already see big improvements in how it guides my focus and design:

  • Forces me to make design decisions, even if only partially or as stubs, for my tests to be relevant
  • Builds on confidence I have on existing solid code...complex logic can be digested in bites now
  • IT's EASY to use!

One thing I had to figure out was implementing my own Trace Listener so I could write out things like property values or other stuff during my tests. Plus,I am doing some tests with my NHibernate Dao classes so I wrote the following little base class for my NHibernate test classes. I have a custom Trace Listener class that I can extend to do more fun stuff later and I wire it up in the Setup() method of the fixture. Plus, I am mocking sessions for each call so I can simulate an ASP.nET Session-per-view strategy. This was inspired by Billy McCafferty and all his great stuff on NHibernate...his blogs are a great resource...anyways, here it is:

[TestFixture()]

[Category("NHibernate Tests")]

public abstract class NHibernateUnitTest

{

private TraceListener listener = new NUnitTracer();

protected TraceListener Listener

{

get

{

return listener;

}

}

[SetUp()]

public virtual void Init()

{

NHibernateSessionManager.Instance.BeginTransaction();

Trace.Listeners.Add(listener);

 

}

/// <summary>

/// Properly disposes of the <see cref="NHibernateSessionManager"/>.

/// This always rolls back the transaction - changes never get committed.

/// This TestFixtureTearDown could be moved to a NHibernateUnitTest class so you don't have to

/// copy/past it into every unit test class.

/// </summary>

[TestFixtureTearDown]

public virtual void Dispose()

{

//NHibernateCore.NHibernateSessionManager.Instance.CommitTransaction();

Trace.Listeners.Remove(Listener);

NHibernateSessionManager.Instance.RollbackTransaction();

}

/// <summary>

/// To simulate a http session attachment, we need to end the session between each test sometimes.

/// </summary>

[TearDown()]

public virtual void EndMockSession()

{

//NHibernateCore.NHibernateSessionManager.Instance.CommitTransaction();

NHibernateSessionManager.Instance.RollbackTransaction();

 

}


Feedback

No comments posted yet.


Post a comment





 

Please add 7 and 1 and type the answer here:

 

 

Copyright © Mike Nichols