Table of Contents

Class TestBase<TBackground>

Namespace
TinyBDD
Assembly
TinyBDD.dll

A strongly-typed base class for TinyBDD tests with typed background state.

public abstract class TestBase<TBackground> : TestBase where TBackground : class

Type Parameters

TBackground

The type of background state produced by background steps.

Inheritance
TestBase<TBackground>
Inherited Members

Examples

public record TestContext(DbConnection Db, User CurrentUser);

[Feature("User Management")]
public class UserTests : TestBase<TestContext>
{
    protected override ScenarioChain<TestContext> ConfigureTypedBackground()
    {
        return Given("database connection", () => new DbConnection())
            .And("admin user", db => new TestContext(db, db.GetAdmin()));
    }

    [Scenario("Admin can view users")]
    [Test]
    public async Task AdminCanViewUsers()
    {
        await GivenBackground()
            .When("listing users", ctx => ctx.Db.GetUsers())
            .Then("users returned", users => users.Any())
            .AssertPassed();
    }
}

Remarks

This base class extends TestBase to provide strongly-typed access to background state. Override ConfigureTypedBackground() instead of ConfigureBackground() to define background steps that produce a specific type.

Use Background to access the typed background state after ExecuteBackgroundAsync(CancellationToken) has been called.

Properties

Background

Gets the typed background state after ExecuteBackgroundAsync(CancellationToken) completes.

protected TBackground Background { get; }

Property Value

TBackground

Exceptions

InvalidOperationException

Thrown if background steps have not been executed.

Methods

ConfigureBackground()

Sealed implementation that delegates to ConfigureTypedBackground().

protected override sealed ScenarioChain<object>? ConfigureBackground()

Returns

ScenarioChain<object>

ConfigureTypedBackground()

Override to configure typed background steps that run before each scenario.

protected abstract ScenarioChain<TBackground> ConfigureTypedBackground()

Returns

ScenarioChain<TBackground>

A ScenarioChain<T> representing the background steps.

Remarks

This method is called by the sealed ConfigureBackground() implementation. The chain should produce the background state but should not call AssertPassed().

GivenBackground()

Starts a Given step with the typed background state.

protected ScenarioChain<TBackground> GivenBackground()

Returns

ScenarioChain<TBackground>

A ScenarioChain<T> starting with the background state.

GivenBackground(string)

Starts a Given step with the typed background state and a custom title.

protected ScenarioChain<TBackground> GivenBackground(string title)

Parameters

title string

A custom title for the Given step.

Returns

ScenarioChain<TBackground>

A ScenarioChain<T> starting with the background state.

See Also