【问题标题】:How to make FsCheck generate random strings that respect MaxLengthAttribute?如何让 FsCheck 生成尊重 MaxLengthAttribute 的随机字符串?
【发布时间】:2018-08-10 06:47:43
【问题描述】:

FsCheck 是否可以生成尊重 MaxLengthAttribute 的随机记录。示例记录类型:

type Person =
    {
        Id: int
        [<System.ComponentModel.DataAnnotations.MaxLength(256)>]
        FirstName: string
        [<System.ComponentModel.DataAnnotations.MaxLength(256)>]
        LastName: string
    }

【问题讨论】:

    标签: .net f# fscheck


    【解决方案1】:

    不是开箱即用,但您可以执行以下操作:

    Arb.generate<Person> 
    |> Gen.where (fun p -> p.FirstName.Length <= 256 && p.LastName.Length <= 256)
    

    然后就是根据传入的事物的类型为Gen.where创建谓词,即使用反射找到具有MaxLength属性的属性,取出值并限制长度。

    另请注意,默认情况下,每次测试生成 100 个值的生成字符串的最大长度为 50,因此这可能没有实际意义。

    【讨论】:

      猜你喜欢
      • 2015-11-07
      • 1970-01-01
      • 1970-01-01
      • 2010-12-07
      • 2011-08-06
      • 1970-01-01
      • 1970-01-01
      • 2014-02-09
      相关资源
      最近更新 更多