Skip to content

Assert unit tests in natural language by specifying expectations.

License

Notifications You must be signed in to change notification settings

Testably/Testably.Expectations

Repository files navigation

Testably.Expectations
Nuget Build Quality Gate Status Codacy Badge Mutation testing badge

This library is used to assert unit tests in natural language by specifying expectations. It tries to take the best from fluentassertions and TUnit and combine them to a new assertions library.

Architecture

Async everything

All expectations are completely async. This allows complete support of IAsyncEnumerable as well es HttpResponseMessage or similar async types. No need to distinguish between action.Should().Throw() and await asyncAction.Should().ThrowAsync()!

Delayed evaluation

By using await, the evaluation is only triggered after the complete fluent chain is loaded, which has some nice benefits:

  • Because can be registered once as a general method that can be applied at the end of the expectation instead of cluttering all methods with the because and becauseArgs parameters
  • WithCancellation can also be registered at the end an applies a CancellationToken to all async methods which allows cancellation of IAsyncEnumerable evaluations
  • Expectations can be combined directly (via Expect.ThatAll) instead of relying on global state (e.g. assertion scopes)

Extensibility

Fluentassertions have a proven way of extensibility via extension methods on .Should(). A similar approach is used here:

  • Extensions can be written for new types (by writing a .Should() extension methods for IExpectSubjectThat<TType>)...
  • and also for existing types (by writing an extension method on IThat<TType>)

Usage

By adding global using static Testably.Expectations.Expect; anywhere in the test project, that await can be part of the sentence of the expectation.

[Fact]
public async Task SomeMethod_ShouldThrowArgumentNullException()
{
  await That(SomeMethod).Should().Throw<ArgumentNullException>()
    .WithMessage("Value cannot be null")
    .Because("we tested the null edge case");
}

Benchmarks

Some simple benchmarks are available here.

About

Assert unit tests in natural language by specifying expectations.

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages