Skip to content

Advanced Lifecycle Options

Anthony Mastrean edited this page Aug 1, 2013 · 8 revisions

The following lifecycle options should be used sparingly and with great purpose and deliberation. There hasn't been much said on these topics, so I'll use examples from my own tests/systems where possible.

IAssemblyContext

You can implement an IAssemblyContext in your spec assembly to set things up before or clean things up after all of your specs in that assembly have run.

public class CultureSensitiveTests : IAssemblyContext
{
    private static readonly CultureInfo Original = Thread.CurrentThread.CurrentCulture;
    private static readonly CultureInfo Deutsch = new CultureInfo("de-DE");

    public void OnAssemblyStart()
    {
        Thread.CurrentThread.CurrentCulture = Deutsch;
        Thread.CurrentThread.CurrentUICulture = Deutsch;
    }

    public void OnAssemblyComplete()
    {
        Thread.CurrentThread.CurrentCulture = Original;
        Thread.CurrentThread.CurrentUICulture = Original;
    }
}

ICleanupAfterEveryContextInAssembly

You can implement an ICleanupAfterEveryContextInAssembly to perform cleanup after every context. Think cleaning up static state, resetting your Clock/DateTime replacement, for example.

SetupForEachSpecification

There is no longer a difference between Establish context and Establish context_once, the context is always established only once. The idea is that observations are observations, they should not mutate state so there is no reason to execute the context more than once. Same for cleanup. If you really want to do execute the context for each specification, first try to change your mind, then if you still want it, use [SetupForEachSpecification] on your context class.

Clone this wiki locally