Context
Sometimes, further context is required throughout a unit test, above and beyond the supplied target class and the target method's return value.

Inside the initializers, you can call SetContext<TContext>(TContext contextValue) method for use inside the action or assertion classes.

public class SessionIsStarted : IInitializer<ISession>, ITearDown<ISession>
{
	public void SetUp(ref ISession session)
	{
		session = SessionFactory.CreateSession();
		SetContext<ITransaction>(session.BeginTransaction());
	}

	public void TearDown(ISession session)
	{
		GetContext<ITransaction>().Commit();
		session.Dispose();
	}
}


From the alpha release, you can now name the context, so that you can have more than one value of the same type:

public class CalculatorIsDefaultConstructed : IInitializer<Calculator>
{
	public void SetUp(ref Calculator calculator)
	{
		calculator = new Calculator();

		this.SetContext(int.MaxValue);
		this.SetContext("one", 1);
		this.SetContext("two", 2);
		this.SetContext("three", 3);
	}
}


Internally, two separate collections are maintained: one for anonymous context (which int.MaxValue will be saved into, associated with the System.Int32 type) and another for named context. When using Initializers which leverage the context, the anonymous collection is always used.

Last edited Aug 15, 2011 at 3:27 PM by garymcleanhall, version 4

Comments

No comments yet.