PulseSharp.Testing
Test helpers for apps that use PulseSharp, including the in-memory store. Reference it from test projects only.
Generated from the XML documentation of
PulseSharp.Testing.
EmittedEvent
public sealed record EmittedEventAn event that was emitted, as a fake or the test host recorded it.
| Parameter | Description |
|---|---|
Event | The event. |
Stream | The stream it was emitted to. |
Options | The options it was emitted with. |
EmittedEvent(object, string, EmitOptions)
An event that was emitted, as a fake or the test host recorded it.
| Parameter | Description |
|---|---|
Event | The event. |
Stream | The stream it was emitted to. |
Options | The options it was emitted with. |
Event
The event.
Stream
The stream it was emitted to.
Options
The options it was emitted with.
EnqueuedJob
public sealed record EnqueuedJobA job that was enqueued, as a fake or the test host recorded it.
| Parameter | Description |
|---|---|
Job | The job. |
RunAt | The earliest time it may run, or null for as soon as possible. |
Options | The options it was enqueued with. |
EnqueuedJob(object, DateTimeOffset?, EnqueueOptions)
A job that was enqueued, as a fake or the test host recorded it.
| Parameter | Description |
|---|---|
Job | The job. |
RunAt | The earliest time it may run, or null for as soon as possible. |
Options | The options it was enqueued with. |
Job
The job.
RunAt
The earliest time it may run, or null for as soon as possible.
Options
The options it was enqueued with.
FakeJobContext
public class FakeJobContextA JobContext for tests. It records the progress a job handler reports. Derive from it to change how it behaves.
FakeJobContext(string, int)
Creates the context.
| Parameter | Description |
|---|---|
jobName | The job's name. The default is test-job. |
attempt | The attempt number: 1 for the first try, 2 for the first retry. The default is 1. |
JobId
JobName
Attempt
Progress
Every progress report, in order.
LastProgress
The last progress report, or null when the handler reported none.
Report(int, int, CancellationToken)
Records the progress.
| Parameter | Description |
|---|---|
done | Items finished. |
total | Items in total. |
cancellationToken | Not used. |
Returns: A completed task.
FakePulse
public class FakePulseAn IPulse for tests: it records what is emitted and enqueued, and runs nothing. Pass it to the code you test, then check it with Emitted, NotEmitted, Enqueued and NotEnqueued, or read FakePulse.Events and FakePulse.Jobs yourself.
Expected versions are checked like the real stores do. Derive from it to change how it behaves.
Example
var pulse = new FakePulse();
var checkout = new Checkout(pulse);
await checkout.Complete(sale);
var completed = pulse.Emitted<SaleCompleted>();
Assert.Equal(25m, completed.Total);
pulse.Enqueued(new SendReceipt(sale.Id));Events
Jobs
ThrowOnEmit
When set, every Emit throws this exception instead of recording, to test what your code does when saving an event fails.
ThrowOnEnqueue
When set, every Enqueue throws this exception instead of recording.
Emit<>(StreamName, T0, CancellationToken)
Emit<>(StreamName, T0, EmitOptions, CancellationToken)
EmitAll(StreamName, IReadOnlyList<object>, CancellationToken)
EmitAll(StreamName, IReadOnlyList<object>, EmitOptions, CancellationToken)
Enqueue<>(T0, CancellationToken)
Enqueue<>(T0, EnqueueOptions, CancellationToken)
Clear()
Forgets everything recorded so far, for example after setting up a test.
HandlerTestExtensions
public static class HandlerTestExtensionsCalls event and job handlers directly in a test, without building a context or passing a token.
Handle<TEvent>(IEventHandler<TEvent>, TEvent, string, int, CancellationToken)
Calls an event handler with an event, as the engine would.
| Type parameter | Description |
|---|---|
TEvent | The event type. |
| Parameter | Description |
|---|---|
handler | The handler. |
event | The event. |
stream | The stream name. The default is the event's name, like pulse.Emit(event) uses. |
attempt | The attempt number: 1 for the first try, 2 for the first retry. The default is 1. |
cancellationToken | Cancels the handler. |
Returns: A task that completes when the handler is done.
Example
await handler.Handle(new SaleCompleted(1, 25m));Handle<TJob>(IJobHandler<TJob>, TJob, int, CancellationToken)
Calls a job handler with a job, as the engine would, and returns the context with the progress it reported.
| Type parameter | Description |
|---|---|
TJob | The job type. |
| Parameter | Description |
|---|---|
handler | The handler. |
job | The job. |
attempt | The attempt number: 1 for the first try, 2 for the first retry. The default is 1. |
cancellationToken | Cancels the handler. |
Returns: The context, with the progress the handler reported.
Example
var context = await handler.Handle(new ExportReport(today));
Assert.Equal((3, 3), context.LastProgress);IRecordedPulse
public interface IRecordedPulseAnything that knows which events were emitted and which jobs were enqueued: FakePulse and PulseTestHost. The checks in PulseAssertions work on it, and so do your own extension methods.
Events
The events emitted, in order.
Jobs
The jobs enqueued, in order.
PulseAssertionException
public sealed class PulseAssertionExceptionThrown when a PulseSharp test check fails, for example ShouldHaveOne or WaitUntilIdle. Every test framework reports it as a failed test, with its message.
PulseAssertionException(string)
Creates the exception.
| Parameter | Description |
|---|---|
message | What was expected and what happened. |
PulseAssertions
public static class PulseAssertionsChecks on FakePulse and PulseTestHost. Each one fails with a message that shows what was expected and everything that really happened. The ones that find something return it, so you can check it further with your usual asserts.
Write your own checks the same way: an extension method on IRecordedPulse that reads IRecordedPulse.Events and IRecordedPulse.Jobs.
Emitted<TEvent>(IRecordedPulse)
Checks that exactly one TEvent was emitted, and returns it.
| Type parameter | Description |
|---|---|
TEvent | The event type. |
| Parameter | Description |
|---|---|
pulse | The fake or the test host. |
Returns: The event.
Throws PulseAssertionException when none was emitted, or more than one.
Emitted<TEvent>(IRecordedPulse, StreamName)
Checks that exactly one TEvent was emitted to a stream, and returns it.
| Type parameter | Description |
|---|---|
TEvent | The event type. |
| Parameter | Description |
|---|---|
pulse | The fake or the test host. |
stream | The stream, for example "Sale-1". |
Returns: The event.
Throws PulseAssertionException when none was emitted to the stream, or more than one.
Emitted<TEvent>(IRecordedPulse, TEvent)
Checks that exactly one event equal to expected was emitted, and returns it. Records are compared by value; other types by their JSON.
| Type parameter | Description |
|---|---|
TEvent | The event type. |
| Parameter | Description |
|---|---|
pulse | The fake or the test host. |
expected | The event you expect. |
Returns: The emitted event.
Throws PulseAssertionException when no such event was emitted, or more than one.
Example
pulse.Emitted(new StockWentNegative("coffee"));Emitted<TEvent>(IRecordedPulse, Func<TEvent, bool>, string)
Checks that exactly one TEvent that matches a condition was emitted, and returns it.
| Type parameter | Description |
|---|---|
TEvent | The event type. |
| Parameter | Description |
|---|---|
pulse | The fake or the test host. |
where | The condition. |
condition | Filled in by the compiler with the condition's code, for the failure message. |
Returns: The event.
Throws PulseAssertionException when no such event was emitted, or more than one.
Example
pulse.Emitted<SaleCompleted>(sale => sale.Total == 25m);AllEmitted<TEvent>(IRecordedPulse, StreamName)
Returns every TEvent that was emitted, in order, without checking anything.
| Type parameter | Description |
|---|---|
TEvent | The event type. |
| Parameter | Description |
|---|---|
pulse | The fake or the test host. |
stream | Only events of this stream, or null for every stream. |
Returns: The events.
NotEmitted<TEvent>(IRecordedPulse, StreamName)
Checks that no TEvent was emitted.
| Type parameter | Description |
|---|---|
TEvent | The event type. |
| Parameter | Description |
|---|---|
pulse | The fake or the test host. |
stream | Only check this stream, or null for every stream. |
Throws PulseAssertionException when one was emitted.
Enqueued<TJob>(IRecordedPulse)
Checks that exactly one TJob was enqueued, and returns it.
| Type parameter | Description |
|---|---|
TJob | The job type. |
| Parameter | Description |
|---|---|
pulse | The fake or the test host. |
Returns: The job.
Throws PulseAssertionException when none was enqueued, or more than one.
Enqueued<TJob>(IRecordedPulse, TJob)
Checks that exactly one job equal to expected was enqueued, and returns it. Records are compared by value; other types by their JSON.
| Type parameter | Description |
|---|---|
TJob | The job type. |
| Parameter | Description |
|---|---|
pulse | The fake or the test host. |
expected | The job you expect. |
Returns: The enqueued job.
Throws PulseAssertionException when no such job was enqueued, or more than one.
Example
pulse.Enqueued(new SendReceipt(saleId));Enqueued<TJob>(IRecordedPulse, Func<TJob, bool>, string)
Checks that exactly one TJob that matches a condition was enqueued, and returns it.
| Type parameter | Description |
|---|---|
TJob | The job type. |
| Parameter | Description |
|---|---|
pulse | The fake or the test host. |
where | The condition. |
condition | Filled in by the compiler with the condition's code, for the failure message. |
Returns: The job.
Throws PulseAssertionException when no such job was enqueued, or more than one.
AllEnqueued<TJob>(IRecordedPulse)
Returns every TJob that was enqueued, in order, without checking anything.
| Type parameter | Description |
|---|---|
TJob | The job type. |
| Parameter | Description |
|---|---|
pulse | The fake or the test host. |
Returns: The jobs.
NotEnqueued<TJob>(IRecordedPulse)
Checks that no TJob was enqueued.
| Type parameter | Description |
|---|---|
TJob | The job type. |
| Parameter | Description |
|---|---|
pulse | The fake or the test host. |
Throws PulseAssertionException when one was enqueued.
PulseSharpInMemoryExtensions
public static class PulseSharpInMemoryExtensionsKeeps PulseSharp's data in memory, for tests.
UseInMemory(PulseSharpBuilder)
Keeps events, cursors and jobs in memory. Everything is lost when the app stops, so use it only in tests. PulseTestHost uses it by default.
| Parameter | Description |
|---|---|
pulse | The PulseSharp builder. |
Returns: The builder.
PulseTestHost
public sealed class PulseTestHostRuns the real PulseSharp engine in a test, in memory by default, and waits until all of its work is done, so tests never need delays.
It is an IPulse itself: emit and enqueue on it directly. Retries are fast (10 ms, doubling) so a failing handler stops quickly; change any setting in the configure action.
Example
await using var pulse = await PulseTestHost.Start(
pulse => pulse.AddHandler<SaleCompleted, UpdateStock>(),
services => services.AddSingleton<IStock, FakeStock>());
await pulse.Given("Sale-1", new SaleStarted(1), new ItemAdded("coffee"));
await pulse.Emit(new SaleCompleted(1, 25m));
await pulse.WaitUntilIdle();
pulse.Emitted(new StockWentNegative("coffee"));
pulse.NothingFailed();Services
The app's services, for example to get a fake you registered and check what it received.
Control
The control API: cursors, retry, skip, rewind and jobs.
Start(Action<PulseSharpBuilder>, Action<IServiceCollection>)
Starts the engine with the in-memory store, your handlers and your services.
| Parameter | Description |
|---|---|
configure | Adds handlers and changes settings, like in AddPulseSharp. Choose another store here to test with it. |
services | Registers the services your handlers need, for example fakes. Optional. |
Returns: The running test host. Dispose it at the end of the test.
Throws InvalidOperationException when the setup is not valid, or PulseSharp and PulseSharp.Testing have different versions.
Given(StreamName, object[])
Sets up the starting state: saves events to a stream, then waits until the handlers have handled them. These events, and what the handlers do with them, are not part of PulseTestHost.Events and PulseTestHost.Jobs afterwards.
| Parameter | Description |
|---|---|
stream | The stream, for example "Sale-1". |
events | The events, in order. |
Returns: A task that completes when the handlers are done.
Given(object[])
Sets up the starting state: saves each event to the stream named after its type, like pulse.Emit(event), then waits until the handlers have handled them.
| Parameter | Description |
|---|---|
events | The events, in order. |
Returns: A task that completes when the handlers are done.
WaitUntilIdle(TimeSpan?)
Waits until every handler has handled every event, no job is running or due, and nothing is waiting for a retry. A handler that failed for good counts as done: check that with PulseTestHost.NothingFailed.
| Parameter | Description |
|---|---|
timeout | How long to wait. The default is 10 seconds. |
Returns: A task that completes when the engine is idle.
Throws PulseAssertionException when the engine was still busy after the timeout. The message says with what.
Events
The events emitted since the start or the last Given, by the test or by handlers, in order. Call PulseTestHost.WaitUntilIdle first to include what handlers emit.
Jobs
The jobs enqueued since the start or the last Given, by the test or by handlers, in order, whatever their state now. Call PulseTestHost.WaitUntilIdle first to include what handlers enqueue.
NothingFailed()
Checks that no handler stopped on a failing event and no job failed. Without this check, a failing handler would go unnoticed in a test.
Throws PulseAssertionException when something failed. The message shows each failure and its error.
Emit<>(StreamName, T0, CancellationToken)
Emit<>(StreamName, T0, EmitOptions, CancellationToken)
EmitAll(StreamName, IReadOnlyList<object>, CancellationToken)
EmitAll(StreamName, IReadOnlyList<object>, EmitOptions, CancellationToken)
Enqueue<>(T0, CancellationToken)
Enqueue<>(T0, EnqueueOptions, CancellationToken)
DisposeAsync()
Stops the engine and releases everything.
Returns: A task that completes when the engine has stopped.