2

我现在正在尝试通过 FsCheck 的示例来处理具有歧视联合的类型,以便为我们的大型项目建立最佳实践。现在我从我的生成器中得到空值,我不知道为什么。在以下代码中,DataGen.containerGenerator 为空。

namespace Container
open System
open Xunit
open FsCheck

module ContainerLibrary = 
    type [<Measure>] oz

    type Container = 
        | Cup of Common
        | Bowl of Common
    and Common = 
        { Volume    :decimal<oz>
          Weight    :decimal}

module DataGen = 
    type Generators = 
        static member arbVolume = 
            FsCheck.Gen.choose (1, 16)
            |> FsCheck.Gen.map(fun x -> (decimal x / 8.0M) * 1.0M<ContainerLibrary.oz>)
            |> FsCheck.Arb.fromGen

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

    let bowlGenerator = 
        FsCheck.Gen.map2 (fun a b -> ContainerLibrary.Bowl( { Volume = a 
                                                              Weight = b})) 
                         (Generators.arbVolume.Generator) 
                         (FsCheck.Arb.generate<decimal>)
    let cupGenerator =
        FsCheck.Gen.map2 (fun a b -> ContainerLibrary.Cup( { Volume = a 
                                                             Weight = b})) 
                         (Generators.arbVolume.Generator) 
                         (FsCheck.Arb.generate<decimal>)

    let containerGenerator =
        Gen.oneof [bowlGenerator; cupGenerator]

module Tests =
    [<Fact;>]
    let ``01 : Containers must be no more than 20 oz`` () =
        //Is this the best way to get one of something?
        let c = FsCheck.Gen.sample 0 1 DataGen.containerGenerator |> Seq.head
        Assert.NotNull (c)
4

1 回答 1

2

当我运行它时,它似乎并不为空,即使我得到更多的值。您使用的是哪个版本的 FsCheck?

[<Fact;>]
let ``01 : Containers must be no more than 20 oz`` () =
    //Is this the best way to get one of something?
    Gen.sample 0 100 DataGen.containerGenerator |> Seq.iter(fun c -> printf "%A" c; Assert.NotNull (c))

无论如何,关于你在做什么,有几件事需要注意。

  • FsCheck 使用反射来注册生成器;并且无法通过反射看到测量类型参数的类型。因此,对于所有小数,Arb.register 实际上会覆盖小数生成器。
  • 不知何故,FsCheck。你用的资格混淆了intellisense 没完没了。
  • Gen.sample 是测试生成器的合理方法,但我主要在交互式设置中使用它;如果您在设置测试时遇到了麻烦,我倾向于使用 FsCheck 内置的测试用例观察功能。请参阅此处的“观察测试用例分布”:https ://fsharp.github.io/FsCheck/Properties.html
  • 像您正在做的那样在模块初始化中使用 Arb.register 有点脆弱,这取决于 F# 中的模块初始化规则来注册生成器。如果您使用的是 Xunit,最好使用内置集成来减少这方面不可避免的挫败感。

考虑到其中一些事情,我已经重写了您的示例:

module DataGen = 

open ContainerLibrary

//can't really register this one because of the measure, would override all decimal generatos
let volumeGenerator = 
        Gen.choose (1, 16)
        |> Gen.map(fun x -> (decimal x / 8.0M) * 1.0M<ContainerLibrary.oz>)

let commonGenerator =
    Gen.map2 (fun a b -> { Volume = a 
                           Weight = b})
                     (volumeGenerator) 
                     (Arb.generate<decimal>)

//in case you like applicative style, otherwise completely equivalent
let commonGeneratorAlternative =
    (fun a b -> { Volume = a; Weight = b}) <!> volumeGenerator <*> Arb.generate<decimal>

let bowlGenerator = Gen.map Bowl commonGenerator
let cupGenerator = Gen.map Cup commonGenerator

let containerGenerator =
    Gen.oneof [bowlGenerator; cupGenerator]

type Generators =
    static member Container() = containerGenerator |> Arb.fromGen

module Tests =
open FsCheck.Xunit
open ContainerLibrary

//use PropertyAttribute from FsCheck.Xunit
//use the defined container generator - can also move this to module level
//other ways to parametrize
[<Property(Arbitrary=[|typeof<DataGen.Generators>|])>]
//thanks to PropertyAttribute can now just take container as argument
let ``01 : Containers must be no more than 20 oz`` (container:Container) =
    match container with
    | Cup common
    | Bowl common -> common.Volume <= 20.0M<oz>
    |> Prop.collect container //see the generated values in the output

这会输出如下内容:

好的,通过了 100 次测试。

1% 杯 {Volume = 2.0M; 重量 = -0.0000221360928858744815609M;}。1% 杯 {体积 = 1.8750M; 重量 = 922337.20325598085121M;}。ETC

于 2015-01-06T09:23:34.980 回答