这可能是非常简单的事情,但我对FsCheck并不熟悉,也不知道为什么下面的内容会引发它所做的错误("Geneflect: type not System.Numerics.BigInteger")。
open System.Numerics
type NumericGenerator =
/// Generating BigIntegers (though only in the regular integer range for now)
static member BigInt() =
{ new Arbitrary<System.Numerics.BigInteger>() with
override x.Generator =
Arb.generate<int>
|> Gen.map (fun i -> new BigInteger(i)) }
[<Property>]
let ``Simple test`` (b: BigInteger) =
Arb.register<NumericGenerator> |> ignore
b + 1I = 1I + b这是使用FsCheck和xUnit集成。
发布于 2014-07-21 11:55:15
FsCheck试图在调用测试之前生成一个BigInteger,因为Arb.register调用位于测试方法本身中。然后,它试图通过反射来做到这一点,而反射失败了。
您可以将自定义任意实例作为参数添加到属性中,从而告知FsCheck。
[<Property(Arbitrary=[|typeof<NumericGenerator>|])>]此外,还可以将ArbitraryAttribute添加到测试的封闭模块中,以便为模块中的所有属性注册该任意实例。有关一些示例,请参见https://github.com/fsharp/FsCheck/blob/master/tests/FsCheck.Test/Runner.fs。
最后一个技巧--如果您正在生成一个很容易转换为/从另一个已经生成的类型转换的类型,那么您可以使用Arb.convert方法轻松地创建一个generate和一个收缩器。类似于:
Arb.Default.Int32() |> Arb.convert ...该工作了。
https://stackoverflow.com/questions/24857008
复制相似问题