The "future" assertion library for .NET, promise!
This library is there to help you do assertions on events that are about to happen in the near future. For example, when building integration tests for a subscriber on a service bus.
See the docs for the changed API since v3
.
See some notes on using System.Linq.Async
and FluentAssertions
.
Define your hypothesis with an observer, an experiment, an a time constraint. Feed the observer with samples and validate the hypothesis:
var observer = Observer.For<int>();
You feed the observer with data:
await observer.Add(3);
For example with an injected stub:
var service = Substitute.For<SomeInjectable>();
service
.When(x => x.Demo(Arg.Any<int>()))
.Do(x => observer.Add(x.Arg<int>()));
or with a hand-rolled implementation:
class TestAdapter(Observer<int> observer) : SomeInjectable
{
public Task Demo(int data) =>
observer.Add(data);
}
var service = new TestAdapter(observer);
or with the consumer factory Hypothesist.MassTransit when using MassTransit:
cfg.ReceiveEndpoint("...", x => x.Consumer(observer.AsConsumer));
or with the handler factory Hypothesist.Rebus when using Rebus:
using var activator = new BuiltinHandlerActivator()
.Register(observer.AsHandler);
Just checkout the available adapters for more information!
You validate if your hypothesis holds true for the supplied samples during the specified time window.
await hypothesis
.On(observer)
.Timebox(1.Seconds());
.Any()
.Match(1234)
.Validate();
But somewhere in between you've fired off the eventing mechanism that ultimately invokes the injected service.
The two parts of the hypothesis are the experiment and a time constraint.
Validates that at least one item matches the assertion, meaning the experiment stops when this item is observed.
Validates that all items that are observed during the experiment matches the assertion.
Remark: having no items observed during the time window also means the hypothesis holds true;
Validates that the first item that is observed matches the assertion.
Validates that exactly one item is observed that matches the assertion.
Remark: having other items not matching the assert means the hypothesis still holds true.
Validates that exactly the given number of occurrences is observed that matches the assertion within the given timeframe.
Validates that at least the given number of occurrences is observed that matches the assertion.
Validates that at most the given number of occurrences is observed that matches the assertion.
Some adapters wrapping around the hypothesis to make invocation convenient: