Skip to content

Object disposal

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

xWellBehaved.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 xWellBehaved.net for disposal immediately after all the steps in the scenario have been executed. xWellBehaved.net also guarantees disposal in the event of unhandled exceptions, rather like a C# using { } block.

For instance, 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.

public class Calculator : IDisposable { ... }
[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(context => calculator = new Calculator().Using(context));

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

    "Then the answer is 3".x(() => answer.AssertEqual(3));
}

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

Clone this wiki locally