logo

Idioms.FsCheck

AutoFixture 3.18 introduces a new glue library called Idioms.FsCheck.

It uses FsCheck to implement a reusable assertion that verifies (or at least, makes probable) that a method doesn’t return null:

[Theory, UnitTestConventions]
public void Scenario(ISpecimenBuilder builder)
{
    var sut = from x in new Methods<SomeClass>()
              select x.SomeMethod();
    var assertion = new ReturnValueMustNotBeNullAssertion(builder);
    Assert.Throws<ReturnValueMustNotBeNullException>(() =>
        assertion.Verify(sut));
}

How it works

If a method is parameterless the assertion simply exercises its return value by invoking it. Otherwise, its return value is exercised against arbitrary values generated by FsCheck.

An injected instance of an ISpecimenBuilder (e.g. a Fixture instance) creates instances of the type containing the method if it’s declared as an instance method.

When needed, AutoFixture and FsCheck can be customized via their DSLs.

Samples

Installation

Idioms.FsCheck is available on NuGet:

PM> Install-Package AutoFixture.Idioms.FsCheck