Class Ambient
- Namespace
- TinyBDD
- Assembly
- TinyBDD.dll
Provides access to the ambient ScenarioContext used by the fluent Flow API.
public static class Ambient
- Inheritance
-
Ambient
- Inherited Members
Examples
// Create a context and make it ambient for Flow.* calls
var previous = Ambient.Current.Value;
Ambient.Current.Value = Bdd.CreateContext(this);
try
{
await Flow.Given(() => 1)
.When("double", x => x * 2)
.Then("> 0", v => v > 0);
}
finally
{
// Always restore to avoid leaking context across tests
Ambient.Current.Value = previous;
}
Remarks
TinyBDD supports two entry points: the explicit Bdd API where a ScenarioContext is passed around, and the ambient Flow API where a context is obtained from this static holder. Test frameworks can set Current at the beginning of a test to avoid plumbing the context through method parameters.
If you use Flow methods without assigning a value to Current, TinyBDD will throw an InvalidOperationException to signal that no scenario is active. You can set it explicitly or inherit from one of the TinyBDD.* base classes that manage it for you.
The value is stored in an AsyncLocal<T> so each asynchronous execution flow (for example, within Task continuations) observes its own value. This makes it safe to use in parallel tests where multiple scenarios run concurrently.
Fields
Current
The ambient scenario context for the current async flow.
public static readonly AsyncLocal<ScenarioContext?> Current
Field Value
Remarks
This field uses AsyncLocal<T> so that each asynchronous execution flow observes its own value. It is safe to use in parallel tests.
Framework adapters like TinyBDD.Xunit/NUnit/MSTest set this automatically for the duration of a test. If you set it manually, prefer the save/restore pattern shown in the example to avoid leaking state between tests.