Skip to content

TearDowns

Michael W Powell edited this page Dec 8, 2020 · 1 revision

xWellBehaved.net now also allows the addition of tear down steps, exactly converse to the Background methods.

Consider that the perspicuous Calculator has the added responsibility of requiring a CoolDown before finally being disposed of. But let us also consider that these are best managed during separately derived test classes.

public abstract class CalculatorFeature
{
    protected Calculator Instance { get; private set; }

    [Background]
    public void Background()
    {
        "Given a calculator".x(() => this.Instance = new Calculator());
    }

    [TearDown]
    public void TearDown() // which may be named anything
    {
        "Dispose of the calculator".x(() => this.Instance.AssertNotNull()?.Dispose());
    }

    [Scenario]
    public void Feature()
    {
        "Just the facts ma'am, we cool".x(() => ...);
    }
}

public class CalculatorCoolDownFeature : CalculatorFeature
{
    [TearDown]
    public void CoolDownTearDown()
    {
        "Cool it, calculator".x(() => this.Instance.AssertNotNull()?.CoolDown());
    }

    [Scenario]
    public void SomeUltraHotFeature()
    {
        "Can we ask you to cooldown after this one".x(() => ...);
    }
}

In the above example, xWellBehaved.net will ensure that CoolDownTearDown occurs before TearDown, in the order opposite that of its declaration.

Clone this wiki locally