Teardown - adamralph/xbehave.net GitHub Wiki

xBehave.net provides an easy way to ensure the execution of teardown code after execution of a scenario.

This is done by simply providing a teardown delegate to the Teardown() extension method. The teardown is then registered by xBehave.net for execution immediately after all the steps in the scenario have been executed. xBehave.net also guarantees execution of the teardown in the event of unhandled exceptions, rather like C# using block or try { } finally { } blocks.

If you have multiple teardowns registered in a scenario, they will be executed in reverse order, rather like a unwinding series of nested C# using or try { } finally { } blocks.

E.g. let us imagine our Calculator is some bizarre steampunk museum piece which needs careful cooling down after usage.

[Scenario]
public void Addition(int x, int y, Calculator calculator, int answer)
{
    "Given the number 1"
        .x(() => x = 1);

    "And the number 2"
        .x(() => y = 2);

    "And a calculator"
        .x(() => calculator = new Calculator())
        .Teardown(() => calculator.CoolDown());

    "When I add the numbers together"
        .x(() => answer = calculator.Add(x, y));

    "Then the answer is 3"
        .x(() => Assert.Equal(3, answer));
}