我有一个Average
类型,其字段count
为int64
,double
字段为sum
。
我使用
生成了一个任意生成有效实例 let AverageGen = Gen.map2 (fun s c -> Average(float(s),int64(int(c))) (Arb.Default.NormalFloat().Generator) (Arb.Default.PositiveInt().Generator) |> Arb.fromGen
如何在xUnit中的Property
样式测试中生成具有Average类型的参数?
[<Property>]
static member average_test(av:Average) = ...
答案 0 :(得分:8)
type Generators =
static member TestCase() =
{ new Arbitrary<TestCase>() with
override x.Generator =
gen { ...
return TestCase(...) }}
[<Property(Arbitrary=[|typeof<Generators>|])>]
答案 1 :(得分:4)
我认为Vasily Kirichenko的解决方案是正确的,但为了完整起见,I've also been able to make it work with this imperative function invocation风格:
do Arb.register<Generators>() |> ignore
......如果你假设一个Generators
课程,就像在Vasily Kirichenko的答案中那样。
编辑,很久以后......
虽然如上所述,强制性方法可能有效,我从不使用因为其不纯净的性质。相反,我有时会use the Arbitrary directly from within the test。上面的AverageGen
值(我将重命名为averageGen
,因为值应该是camelCased),它可能如下所示:
[<Property>]
let member average_test () =
Prop.forAll averageGen (fun avg ->
// The rest of the test goes here... )