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



我有一个 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)

我不想手动设置 方向 = In 或 方向 = 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

编辑:另一种方法

我只是想到,由于这两个法案是独立的,它们可以单独生成,这样就不需要将生成器压缩在一起,您可以根据需要获得不同的类型:

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)       

分别生成两个账单的解决方案效果不佳

最新更新