Skip to content

Latest commit

 

History

History
137 lines (87 loc) · 3.85 KB

README.md

File metadata and controls

137 lines (87 loc) · 3.85 KB

nuget codecov stryker build status

Hypothesist

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.

v3

See the docs for the changed API since v3.

Rx

See some notes on using System.Linq.Async and FluentAssertions.

Usage

schema

Define

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>();

Test

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!

Validate

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.

Experiments

The two parts of the hypothesis are the experiment and a time constraint.

Any

Validates that at least one item matches the assertion, meaning the experiment stops when this item is observed.

All

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;

First

Validates that the first item that is observed matches the assertion.

Single

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.

Exactly

Validates that exactly the given number of occurrences is observed that matches the assertion within the given timeframe.

AtLeast

Validates that at least the given number of occurrences is observed that matches the assertion.

AtMost

Validates that at most the given number of occurrences is observed that matches the assertion.

Adapters

Some adapters wrapping around the hypothesis to make invocation convenient: