Skip to content
This repository has been archived by the owner on Jun 12, 2021. It is now read-only.

Object disposal

Adam Ralph edited this page Feb 23, 2018 · 11 revisions

xBehave.net provides an easy way to ensure an object implementing IDisposable is disposed after execution of a scenario.

This is done by simply calling the Using() extension method immediately after creation of the IDisposable object. The object is then registered by xBehave.net for disposal immediately after all the steps in the scenario have been executed. xBehave.net also guarantees disposal in the event of unhandled exceptions, rather like a C# using { } block.

E.g. let us imagine our Calculator is some bizarre steampunk museum piece which needs careful cooling down after usage and its Dispose() method does this for us.

[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(c => calculator = new Calculator().Using(c));

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

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

c is a context object managed by xBehave.net. Simply pass it through to the Using() extension method.

Clone this wiki locally