如何使用 FsCheck 生成器生成两条相同类型的记录,其中一条记录的 属性 与另一条记录不同

How to use FsCheck generator to generate two records of same type where one record's property differs from the other

我有这个 fscheck nunit 测试生成两条记录,然后我必须更新这两条记录,以便两条记录始终具有不同的方向值 属性

[<Property( Verbose = true )>]
let ``calculate Net Worth 2`` (first:Bill,second:Bill) =
  let owing = { first with Direction = Out }
  let payCheck = { second with Direction = In }

  let compositeBill = {
    Bills = [| owing; payCheck |] 
  }
  let netWorth = calculateNetWorth compositeBill
  Assert.AreEqual(payCheck.Amount - owing.Amount,netWorth)

我不想手动设置 Direction = In 或 Direction = In ,我想使用生成器来指定它。

这样的发电机会是什么样子?

我想留下这样的代码

[<Property( Verbose = true )>]
let ``calculate Net Worth 2`` (first:Bill,second:Bill) =
  let compositeBill = {
    Bills = [| owing; payCheck |] 
  }
  let netWorth = calculateNetWorth compositeBill
  Assert.AreEqual(payCheck.Amount - owing.Amount,netWorth)

这是我试过的方法,但没有成功

type BillsGen =
    static member Bill () =        
        let debit = 
          Arb.generate<Bill>
          |> Gen.map (fun dt -> { dt with Direction = Out} )          
        let credit = 
          Arb.generate<Bill>
          |> Gen.map (fun dt -> { dt with Direction = In} )
        Gen.oneof[ debit; credit ]        

[<SetUp>]
let setup () =
    do Arb.register<BillsGen>() |> ignore 

谢谢

这是我的一些类型

   type Direction = 
    | In  
    | Out

   type Bill = {
     Direction : Direction
   }

   type CompositeBill = {
    Bills : Bill [] 
   }

我对基于 属性 的测试没有经验,但我认为您可以通过创建一个代表您的受限输入值的新类型来做到这一点。然后,您可以在生成器中使用 Gen.zip 来组合两个生成器来构建该类型的值。

type BillsInOut = BillsInOut of Bill * Bill

type BillsInOutGen =
    static member BillsInOut () =
        { new Arbitrary<BillsInOut>() with
            override x.Generator =
                let credit =
                    Arb.generate<Bill>
                    |> Gen.map (fun dt -> { dt with Direction = In })
                let debit =
                    Arb.generate<Bill>
                    |> Gen.map (fun dt -> { dt with Direction = Out })

                Gen.zip credit debit |> Gen.map BillsInOut }

一旦你 运行 Arb.register<BillsInOutGen>(),你就可以将这个新类型作为你的测试参数,这个 属性 应该成立:

let property (BillsInOut (inBill, outBill)) =
    inBill.Direction = In && outBill.Direction = Out

编辑:另一种方法

突然想到,由于这两个bill是独立的,所以可以单独生成,这样就不用把生成器zip在一起,可以根据需要得到不同的种类:

type BillIn = BillIn of Bill
type BillOut = BillOut of Bill

type BillInOutGen =
    static member BillIn () =
        { new Arbitrary<BillIn>() with
            override x.Generator =
                Arb.generate<Bill> |> Gen.map (fun dt -> BillIn { dt with Direction = In }) }
    static member BillOut () =
        { new Arbitrary<BillOut>() with
            override x.Generator =
                Arb.generate<Bill> |> Gen.map (fun dt -> BillOut { dt with Direction = Out }) }

Arb.register<BillInOutGen>()

使用这些的 属性 现在看起来像这样:

let property (BillIn inBill) (BillOut outBill) =
    inBill.Direction = In && outBill.Direction = Out

@thequickbrownfox 的回答很有帮助。但我不得不在测试中做出一些改变。我必须创建类型 BillsInOut = BillsInOut of Bill * Bill 并指定 类 来进行我的测试,然后一切正常 解决方法如下图

type BillsInOut = BillsInOut of Bill * Bill

type BillsInOutGen =
  static member BillsInOut () =
    { 
     new Arbitrary<BillsInOut>() with
      override x.Generator =
        let credit =
          Arb.generate<Bill>
          |> Gen.map (fun dt -> { dt with Direction = In })
        let debit =
          Arb.generate<Bill>
          |> Gen.map (fun dt -> { dt with Direction = Out })        
        Gen.zip credit debit |> Gen.map BillsInOut 
    }


[]
type ``when analysing bills``() =

  [<SetUp>]
  member x.SetUp() = 
   Arb.register<BillsInOutGen>() |> ignore

  [<Property( Verbose = true )>]
  member x.``it should calculate net worth`` (BillsInOut (payCheck, owing)) = 
    Assert.True(payCheck.Direction = In && owing.Direction = Out)       

分别生成两个账单的方案不太好