Class ScenarioBuilder<T>
Provides a fluent arrange/act/assert pipeline for a tracked scenario target.
Inheritance
ScenarioBuilder<T>
Assembly: FastMoq.Core.dll
Syntax
public sealed class ScenarioBuilder<T> where T : class
Type Parameters
Examples
In MockerTestBase<TComponent>, prefer the parameterless overloads when Component is already available.
This example uses the scenario pipeline together with provider-first verification.
Scenario
.When(() => Component.Submit(42))
.Then(() => Component.Channel.Should().Be("priority"))
.Verify<IOrderGateway>(x => x.Publish(42), TimesSpec.Once)
.VerifyNoOtherCalls<IOrderGateway>()
.Execute();
Outside MockerTestBase<TComponent>, use the target-instance overloads.
await mocker.Scenario(service)
.When(component => component.RunAsync())
.Verify<IBackgroundQueue>(x => x.Enqueue("job-42"), TimesSpec.Once)
.ExecuteAsync();
Properties
Instance
Gets the current scenario target instance.
Declaration
public T Instance { get; }
Property Value
Methods
Execute()
Convenience execute for synchronous test bodies.
Declaration
ExecuteAsync()
Executes the scenario (arrange -> act -> assert).
Declaration
public Task ExecuteAsync()
Returns
ExecuteThrowsAsync<TException>()
Executes the scenario and returns the expected exception.
Declaration
public Task<TException> ExecuteThrowsAsync<TException>() where TException : Exception
Returns
| Type |
Description |
| Task<TException> |
|
Type Parameters
| Name |
Description |
| TException |
|
Examples
var exception = await mocker.Scenario(service)
.When(component => component.RunAsync())
.ExecuteThrowsAsync<InvalidOperationException>();
ExecuteThrows<TException>()
Executes the scenario and returns the expected exception.
Declaration
public TException ExecuteThrows<TException>() where TException : Exception
Returns
| Type |
Description |
| TException |
|
Type Parameters
| Name |
Description |
| TException |
|
Examples
Use this form when the exception object itself is the primary assertion target.
var exception = Scenario
.When(() => Component.SendRemindersAsync(now, CancellationToken.None))
.ExecuteThrows<InvalidOperationException>();
exception.Message.Should().Be("SMTP unavailable");
Then(Action)
Declaration
public ScenarioBuilder<T> Then(Action assertion)
Parameters
| Type |
Name |
Description |
| Action |
assertion |
|
Returns
Then(Action<Mocker, T>)
Declaration
public ScenarioBuilder<T> Then(Action<Mocker, T> assertion)
Parameters
Returns
Then(Action<T>)
Adds a synchronous assertion step using only the component instance.
Declaration
public ScenarioBuilder<T> Then(Action<T> assertion)
Parameters
| Type |
Name |
Description |
| Action<T> |
assertion |
|
Returns
Then(Func<Mocker, T, Task>)
Adds an asynchronous assertion step.
Declaration
public ScenarioBuilder<T> Then(Func<Mocker, T, Task> assertion)
Parameters
Returns
Then(Func<Task>)
Adds an asynchronous assertion step.
Declaration
public ScenarioBuilder<T> Then(Func<Task> assertion)
Parameters
| Type |
Name |
Description |
| Func<Task> |
assertion |
|
Returns
Then(Func<T, Task>)
Adds an asynchronous assertion step using only the component instance.
Declaration
public ScenarioBuilder<T> Then(Func<T, Task> assertion)
Parameters
| Type |
Name |
Description |
| Func<T, Task> |
assertion |
|
Returns
VerifyNoOtherCalls<TMock>()
Queues a provider-first VerifyNoOtherCalls step to run during scenario execution.
Declaration
public ScenarioBuilder<T> VerifyNoOtherCalls<TMock>() where TMock : class
Returns
Type Parameters
Verify<TMock>(Expression<Action<TMock>>, TimesSpec?)
Queues a provider-first verification step to run during scenario execution.
Declaration
public ScenarioBuilder<T> Verify<TMock>(Expression<Action<TMock>> expression, TimesSpec? times = null) where TMock : class
Parameters
Returns
Type Parameters
When(Action)
Adds a synchronous act step.
Declaration
public ScenarioBuilder<T> When(Action act)
Parameters
| Type |
Name |
Description |
| Action |
act |
|
Returns
When(Action<Mocker, T>)
Adds a synchronous act step.
Declaration
public ScenarioBuilder<T> When(Action<Mocker, T> act)
Parameters
Returns
When(Action<T>)
Adds a synchronous act step using only the component instance.
Declaration
public ScenarioBuilder<T> When(Action<T> act)
Parameters
| Type |
Name |
Description |
| Action<T> |
act |
|
Returns
When(Func<Mocker, T, Task>)
Adds an asynchronous act step.
Declaration
public ScenarioBuilder<T> When(Func<Mocker, T, Task> act)
Parameters
Returns
When(Func<Task>)
Adds an asynchronous act step.
Declaration
public ScenarioBuilder<T> When(Func<Task> act)
Parameters
Returns
When(Func<T, Task>)
Adds an asynchronous act step using only the component instance.
Declaration
public ScenarioBuilder<T> When(Func<T, Task> act)
Parameters
Returns
WhenThrows<TException>(Action)
Adds a synchronous act step that is expected to throw the specified exception type.
Declaration
public ScenarioBuilder<T> WhenThrows<TException>(Action act) where TException : Exception
Parameters
| Type |
Name |
Description |
| Action |
act |
|
Returns
Type Parameters
| Name |
Description |
| TException |
|
Examples
Use this when the act step is expected to fail but trailing assertions should still run.
Scenario
.WhenThrows<InvalidOperationException>(() => Component.SendRemindersAsync(now, CancellationToken.None))
.Then(() => auditTrail.Count.Should().Be(1))
.Execute();
WhenThrows<TException>(Action<Mocker, T>)
Adds a synchronous act step using the mocker and component instance that is expected to throw the specified exception type.
Declaration
public ScenarioBuilder<T> WhenThrows<TException>(Action<Mocker, T> act) where TException : Exception
Parameters
Returns
Type Parameters
| Name |
Description |
| TException |
|
WhenThrows<TException>(Action<T>)
Adds a synchronous act step using only the component instance that is expected to throw the specified exception type.
Declaration
public ScenarioBuilder<T> WhenThrows<TException>(Action<T> act) where TException : Exception
Parameters
| Type |
Name |
Description |
| Action<T> |
act |
|
Returns
Type Parameters
| Name |
Description |
| TException |
|
WhenThrows<TException>(Func<Mocker, T, Task>)
Adds an asynchronous act step using the mocker and component instance that is expected to throw the specified exception type.
Declaration
public ScenarioBuilder<T> WhenThrows<TException>(Func<Mocker, T, Task> act) where TException : Exception
Parameters
Returns
Type Parameters
| Name |
Description |
| TException |
|
WhenThrows<TException>(Func<Task>)
Adds an asynchronous act step that is expected to throw the specified exception type.
Declaration
public ScenarioBuilder<T> WhenThrows<TException>(Func<Task> act) where TException : Exception
Parameters
Returns
Type Parameters
| Name |
Description |
| TException |
|
Examples
await mocker.Scenario(service)
.WhenThrows<InvalidOperationException>(component => component.RunAsync())
.Then(component => component.Attempts.Should().Be(1))
.ExecuteAsync();
WhenThrows<TException>(Func<T, Task>)
Adds an asynchronous act step using only the component instance that is expected to throw the specified exception type.
Declaration
public ScenarioBuilder<T> WhenThrows<TException>(Func<T, Task> act) where TException : Exception
Parameters
Returns
Type Parameters
| Name |
Description |
| TException |
|
With(Action)
Adds an arrangement action executed before any When/Act operations.
Declaration
public ScenarioBuilder<T> With(Action arrange)
Parameters
| Type |
Name |
Description |
| Action |
arrange |
|
Returns
With(Action<Mocker, T>)
Adds an arrangement action executed before any When/Act operations.
Declaration
public ScenarioBuilder<T> With(Action<Mocker, T> arrange)
Parameters
Returns
With(Action<T>)
Adds an arrangement action using only the component instance.
Declaration
public ScenarioBuilder<T> With(Action<T> arrange)
Parameters
| Type |
Name |
Description |
| Action<T> |
arrange |
|
Returns
With(Func<Mocker, T, Task>)
Adds an asynchronous arrangement action executed before any When/Act operations.
Declaration
public ScenarioBuilder<T> With(Func<Mocker, T, Task> arrange)
Parameters
Returns
With(Func<Task>)
Adds an asynchronous arrangement action executed before any When/Act operations.
Declaration
public ScenarioBuilder<T> With(Func<Task> arrange)
Parameters
| Type |
Name |
Description |
| Func<Task> |
arrange |
|
Returns
With(Func<T, Task>)
Adds an asynchronous arrangement action using only the component instance.
Declaration
public ScenarioBuilder<T> With(Func<T, Task> arrange)
Parameters
| Type |
Name |
Description |
| Func<T, Task> |
arrange |
|
Returns
With(T)
Replaces the scenario target instance.
This enables fluent patterns such as Scenario.With(Component).
Declaration
public ScenarioBuilder<T> With(T instance)
Parameters
| Type |
Name |
Description |
| T |
instance |
|
Returns
Extension Methods