2

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

[<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 [] 
   }
4

2 回答 2

2

我对基于属性的测试没有经验,但我认为您可以通过创建一个代表您的受约束输入值的新类型来做到这一点。然后,您可以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
于 2017-06-12T09:54:11.147 回答
1

@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)       

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

于 2017-06-14T05:05:50.543 回答