Auto Disposal

Any type that implements IDisposable or IAsyncDisposable are auto disposed by the DSL.

Return the type directly; no using block is needed around the DSL.


private sealed class SomeDisposableType : IDisposable
{
    public bool IsDisposed { get; private set; }

    public void Dispose()
    {
        IsDisposed = true;
    }
}

[Fact(DisplayName = "Auto disposal is still standard")]
public async Task ExampleTest1()
{
    // disposable type without a using
    var disposableType = new SomeDisposableType();

    // won't be disposed within the DSL
    await disposableType.Arrange().Act(data => data.IsDisposed).Assert(Assert.False);

    // it will be disposed after
    Assert.True(disposableType.IsDisposed);
}

If you want to opt out of this behaviour call NoDisposal which will stop disposal.


[Fact(DisplayName = "Manual disposal is still possible")]
public async Task ExampleTest2()
{
    // disposable type
    var disposableType = new SomeDisposableType();

    await disposableType
        .Arrange()
        .Act(data => data.IsDisposed)
        .Assert(Assert.False)
        // disable auto-disposal
        .NoDisposal();

    // it will not be disposed
    Assert.False(disposableType.IsDisposed);
}