Skip to content

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 ​

csharp
public sealed record EmittedEvent

An event that was emitted, as a fake or the test host recorded it.

ParameterDescription
EventThe event.
StreamThe stream it was emitted to.
OptionsThe options it was emitted with.

EmittedEvent(object, string, EmitOptions) ​

An event that was emitted, as a fake or the test host recorded it.

ParameterDescription
EventThe event.
StreamThe stream it was emitted to.
OptionsThe options it was emitted with.

Event ​

The event.

Stream ​

The stream it was emitted to.

Options ​

The options it was emitted with.

EnqueuedJob ​

csharp
public sealed record EnqueuedJob

A job that was enqueued, as a fake or the test host recorded it.

ParameterDescription
JobThe job.
RunAtThe earliest time it may run, or null for as soon as possible.
OptionsThe options it was enqueued with.

EnqueuedJob(object, DateTimeOffset?, EnqueueOptions) ​

A job that was enqueued, as a fake or the test host recorded it.

ParameterDescription
JobThe job.
RunAtThe earliest time it may run, or null for as soon as possible.
OptionsThe 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 ​

csharp
public class FakeJobContext

A 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.

ParameterDescription
jobNameThe job's name. The default is test-job.
attemptThe 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.

ParameterDescription
doneItems finished.
totalItems in total.
cancellationTokenNot used.

Returns: A completed task.

FakePulse ​

csharp
public class FakePulse

An 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

csharp
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 ​

csharp
public static class HandlerTestExtensions

Calls 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 parameterDescription
TEventThe event type.
ParameterDescription
handlerThe handler.
eventThe event.
streamThe stream name. The default is the event's name, like pulse.Emit(event) uses.
attemptThe attempt number: 1 for the first try, 2 for the first retry. The default is 1.
cancellationTokenCancels the handler.

Returns: A task that completes when the handler is done.

Example

csharp
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 parameterDescription
TJobThe job type.
ParameterDescription
handlerThe handler.
jobThe job.
attemptThe attempt number: 1 for the first try, 2 for the first retry. The default is 1.
cancellationTokenCancels the handler.

Returns: The context, with the progress the handler reported.

Example

csharp
var context = await handler.Handle(new ExportReport(today));
Assert.Equal((3, 3), context.LastProgress);

IRecordedPulse ​

csharp
public interface IRecordedPulse

Anything 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 ​

csharp
public sealed class PulseAssertionException

Thrown 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.

ParameterDescription
messageWhat was expected and what happened.

PulseAssertions ​

csharp
public static class PulseAssertions

Checks 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 parameterDescription
TEventThe event type.
ParameterDescription
pulseThe 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 parameterDescription
TEventThe event type.
ParameterDescription
pulseThe fake or the test host.
streamThe 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 parameterDescription
TEventThe event type.
ParameterDescription
pulseThe fake or the test host.
expectedThe event you expect.

Returns: The emitted event.

Throws PulseAssertionException when no such event was emitted, or more than one.

Example

csharp
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 parameterDescription
TEventThe event type.
ParameterDescription
pulseThe fake or the test host.
whereThe condition.
conditionFilled 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

csharp
pulse.Emitted<SaleCompleted>(sale => sale.Total == 25m);

AllEmitted<TEvent>(IRecordedPulse, StreamName) ​

Returns every TEvent that was emitted, in order, without checking anything.

Type parameterDescription
TEventThe event type.
ParameterDescription
pulseThe fake or the test host.
streamOnly events of this stream, or null for every stream.

Returns: The events.

NotEmitted<TEvent>(IRecordedPulse, StreamName) ​

Checks that no TEvent was emitted.

Type parameterDescription
TEventThe event type.
ParameterDescription
pulseThe fake or the test host.
streamOnly 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 parameterDescription
TJobThe job type.
ParameterDescription
pulseThe 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 parameterDescription
TJobThe job type.
ParameterDescription
pulseThe fake or the test host.
expectedThe job you expect.

Returns: The enqueued job.

Throws PulseAssertionException when no such job was enqueued, or more than one.

Example

csharp
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 parameterDescription
TJobThe job type.
ParameterDescription
pulseThe fake or the test host.
whereThe condition.
conditionFilled 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 parameterDescription
TJobThe job type.
ParameterDescription
pulseThe fake or the test host.

Returns: The jobs.

NotEnqueued<TJob>(IRecordedPulse) ​

Checks that no TJob was enqueued.

Type parameterDescription
TJobThe job type.
ParameterDescription
pulseThe fake or the test host.

Throws PulseAssertionException when one was enqueued.

PulseSharpInMemoryExtensions ​

csharp
public static class PulseSharpInMemoryExtensions

Keeps 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.

ParameterDescription
pulseThe PulseSharp builder.

Returns: The builder.

PulseTestHost ​

csharp
public sealed class PulseTestHost

Runs 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

csharp
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.

ParameterDescription
configureAdds handlers and changes settings, like in AddPulseSharp. Choose another store here to test with it.
servicesRegisters 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.

ParameterDescription
streamThe stream, for example "Sale-1".
eventsThe 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.

ParameterDescription
eventsThe 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.

ParameterDescription
timeoutHow 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.

Released under the MIT License.