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.
Define your hypothesis with an experiment, time constraint and then test with samples:
var hypothesis = Hypothesis
.For<Data>()
.Any(x => x.Value == 1234);
You test your hypothesis by providing samples:
await hypothesis.Test(sample);
For example with an injected stub:
var service = Substitute.For<IDemoService>();
service
.When(x => x.Demo(Arg.Any<Data>()))
.Do(x => hypothesis.Test(x.Arg<Data>()));
or with a hand-rolled implementation:
class TestService : IDemoService
{
private readonly IHypothesis<Data> _hypothesis;
public TestService(IHypothesis<Data> hypothesis) =>
_hypothesis = hypothesis;
public Task Demo(Data data) =>
_hypothesis.Test(data);
}
var service = new TestService(hypothesis);
or with the consumer factory Hypothesist.MassTransit when using MassTransit:
cfg.ReceiveEndpoint("...", x => x.Consumer(hypothesis.AsConsumer));
or with the handler factory Hypothesist.Rebus when using Rebus:
using var activator = new BuiltinHandlerActivator()
.Register(hypothesis.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.Validate(15.Seconds()); // <-- using TimeSpan extension from FluentAssertions
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.
Some adapters wrapping around the hypothesis to make invocation convenient: