2014-03-28 22 views
6

我有一个类型Average与字段count这是积极的int64double字段sum如何在FsCheck中注册任意实例并让xUnit使用它?

我做了与

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风格测试类型平均参数任意?

[<Property>] 
static member average_test(av:Average) = ... 

回答

8
type Generators = 
    static member TestCase() = 
     { new Arbitrary<TestCase>() with 
      override x.Generator = 
       gen { ... 
         return TestCase(...) }} 

[<Property(Arbitrary=[|typeof<Generators>|])>] 
+2

+1不过,应该不会是'新的任意().. .'? –

+0

c#: '[Property(Arbitrary = new Type [] {typeof(Generators)})]' –

4

我认为,瓦西里Kirichenko的解决方案是正确的,但只是为了完整起见,I've also been able to make it work with this imperative function invocation风格:

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

...如果你假设一个Generators类在瓦西里Kirichenko的答案。


编辑,很久以后...

虽然上面,势在必行的做法可以工作,我从来没有使用它,因为它的性质不纯的。相反,我有时use the Arbitrary directly from within the test。随着AverageGen值以上(我将重命名为averageGen,因为值应驼峰格式),它可能是这样的:

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

我从来没有得到Arb.register为我工作。最近我用Expecto得到“MyType类型不是由FsCheck自动处理的,考虑使用另一种类型或者为它写入和注册一个生成器。” 型DomainGenerators = 静态成员的MyType()= {新任意()与 倍率__发电机= ... 。} 做Arb.register ()|>忽略 [ ] ... \t testPropertyWithConfig config10k“equality”<| fun(myType:MyType) - > ... –

+1

现在很确定,我的问题是Expecto中的一个错误。注册在Expecto以外以我期望的方式工作。我将很快记录并提出问题。 –

相关问题