This project is read-only.
ITearDown

In Behavioral Alpha, tear down was coupled with the IInitializer interface to form IInitializerWithTearDown. This lapse broke the the Interface Segregation Principle and has been reversed. I would have marked that interface as obsolete, but it was an alpha release with limited downloads, so I'm afraid I just removed it entirely. So, this is a breaking change.

The point of a tear down is to perform some deinitialization after the unit test's action has been called.

public class SessionHasBeenStarted : IInitializer<ISession>, ITearDown<ISession>
{
	public void SetUp(ref ISession session)
	{
		this.SetContext(session.BeginTransaction());
	}

	public void TearDown(ISession session)
	{
		var transaction = this.GetContext<ITransaction>();
		if(transaction != null)
		{
			transaction.Commit();
		}
		if(session != null)
		{
			session.Dispose();
		}
	}
}


IErrorHandler

But, what if there is an error in the midst of the action call? After all, some unit tests are intended to make the action throw an exception, for example. Well, the answer is the IErrorHandler interface. If an exception is thrown by the test, then all IErrorHandler interfaces registered as part of IInitializerMarker implementations will be invoked.

public class SessionHasBeenStarted : IInitializer<ISession>, ITearDown<ISession>, IErrorHandler<ISession>
{
	public void SetUp(ref ISession session)
	{
		this.SetContext(session.BeginTransaction());
	}

	public void TearDown(ISession session)
	{
		var transaction = this.GetContext<ITransaction>();
		if(transaction != null)
		{
			transaction.Commit();
		}
		if(session != null)
		{
			session.Dispose();
		}
	}

	public void OnError(ISession session)
	{
		var transaction = this.GetContext<ITransaction>();
		if(transaction != null)
		{
			transaction.Rollback();
		}
		if(session != null)
		{
			session.Dispose();
		}
	}
}


It is worth noting that ITearDown and IErrorHandler on their own are not sufficient to register them with a UnitTest - they must be coupled with IInitializer. This is to ensure that setup, tear down and error handling remain symmetric.

Last edited Aug 15, 2011 at 3:26 PM by garymcleanhall, version 6

Comments

No comments yet.