How to register an arbitrary instance in FsCheck and use xUnit?

I have an Average type with a count field that has a positive int64 and double field called sum .

I made an arbitrary one that generates valid instances with

  let AverageGen = Gen.map2 (fun sc -> Average(float(s),int64(int(c))) (Arb.Default.NormalFloat().Generator) (Arb.Default.PositiveInt().Generator) |> Arb.fromGen 

How can I get this to generate arguments with type Average in Property style tests in xUnit?

 [<Property>] static member average_test(av:Average) = ... 
+6
source share
2 answers
 type Generators = static member TestCase() = { new Arbitrary<TestCase>() with override x.Generator = gen { ... return TestCase(...) }} [<Property(Arbitrary=[|typeof<Generators>|])>] 
+8
source

I think that the decision of Vasily Kirichenko is correct, but only for completeness, I was also able to get him to work with this mandatory function call style:

 do Arb.register<Generators>() |> ignore 

... if you assume the Generators class, as in the answer of Vasily Kirichenko.


Edit, much later ...

Although the above approach may work, I never use it because of its unclean nature. Instead, I sometimes used Arbitration directly from the test . With the above value of AverageGen (which I rename to AverageGen , because the values ​​must be camelCased), it might look like this:

 [<Property>] let member average_test () = Prop.forAll averageGen (fun avg -> // The rest of the test goes here... ) 
+4
source

All Articles