FsCheck, I am not getting `Prop.forAll` to work (F#)

169 views Asked by At

I am not coming right with FsCheck, when I try and use Prop.forAll.

I have created two tests to demonstrate what I am doing, expecting them both to fail.

type Data = { value: int }

type NeverAOne =
    static member Data () =
        Arb.generate<int>
        |> Gen.filter (fun x -> x <> 1)
        |> Gen.map (fun x -> { value = x })
        |> Arb.fromGen

[<Property(Arbitrary = [| typeof<NeverAOne> |] )>] (* Fails *)
let ``Test One`` (x: Data) =
    x.value = 1

[<Fact>]
let ``Test Two`` () = (* Passes *)
    Prop.forAll (NeverAOne.Data ()) (fun x -> x.value = 1)

In this sample, Test Two passes. If I add breakpoints, I can see it is because no data is generated, so it iterates through 0 samples, which means none fail.

I am convinced that I am using Prop.forAll wrong, but though everything I have read through, I cannot find it.

1

There are 1 answers

3
Brian Berns On BEST ANSWER

If you mark the test as a plain Xunit Fact (rather than as a FsCheck Property), you have to explicitly check the property:

[<Fact>]
let ``Test Two`` () =
    let prop = Prop.forAll (NeverAOne.Data ()) (fun x -> x.value = 1)
    Check.QuickThrowOnFailure prop

The result I get is then:

System.Exception : Falsifiable, after 1 test (0 shrinks) (StdGen (74764374, 296947750)):
Original:
{ value = -2 }

Or you can just mark the test as a Property, of course:

[<Property>]
let ``Test Three`` () =
    Prop.forAll (NeverAOne.Data ()) (fun x -> x.value = 1)

Related Questions in F#