Search code examples
c#fscheck

FsCheck c# When property combinator


I'm trying to adopt fscheck, but have a very hard time as there is no much documentation for C#. Can you explain, why the following example of using When combinator for properties fails (evidently, I don't understand how to use it properly)?

 [Test]
    public void WherePorperty()
    {
      Prop.ForAll(NotNullString().ToArbitrary(), s=>s.StartsWith("A").When(s.StartsWith("A"))).VerboseCheckThrowOnFailure();
    }

    public Gen<string> NotNullString()
    {
      return Arb.Generate<string>().Where(s => s != null);
    }

Solution

  • It doesn't actually really fail, it just says "Arguments exhausted after n tests".

    When you use When FsCheck keeps track of how many generated values it has had to throw away because they don't satisfy the condition given in the When. By default this is 1000 values.

    This indicates the condition is too stringent, the generator doesn't generate values that satisfy the When condition often enough.

    It's just kind of a safety net so test time doesn't balloon, or the test gets stuck altogether.

    By the way, this is explained here: https://fscheck.github.io/FsCheck/Properties.html#Conditional-Properties with a C# example.