【问题标题】:Using Bogus to set member property value range使用 Bogus 设置成员属性值范围
【发布时间】:2021-12-01 11:21:43
【问题描述】:

我有以下课程:

  Class Person
    Property ID As String = Nothing
    Property Firstname As String = ""
    Property Lastname As String = ""
  End Class

  Class Account
    Property AccountNumber As String = ""
    Property Owners As New List(Of Person)
  End Class

使用Bogus,我为 Person.ID 设置了从 1,000 到 10,000 的值范围,如下所示:

    Dim fakePerson = New Faker(Of Person)().
      StrictMode(False).
      Rules(Sub(c, p)
              p.ID = c.Random.Long(1000, 10000).ToString
            End Sub
      )

当我像这样实例化 Account 类的实例时,如何设置 Account.Owners 以利用 fakePerson 中定义的 Person.ID 值?:

    Dim fk = Faker.Create()
    Dim acct = fk.Generate(Of Account)

【问题讨论】:

    标签: nested faker bogus


    【解决方案1】:

    由伪造作者 bchavez 在https://github.com/bchavez/Bogus/issues/394 提供的解决方案。

    Sub Main
       Dim personFaker = New Faker(Of Person)
       personFaker.RuleFor(Function(p) p.Firstname, Function(f) f.Name.FirstName)
                  .RuleFor(Function(p) p.Lastname, Function(f) f.Name.LastName)
                  .RuleFor(Function(p) p.ID, Function(f) f.Random.Int(1000,10000).ToString)
    
       Dim accountFaker = New Faker(Of Account)
       accountFaker.RuleFor(Function(a) a.AccountNumber, Function(f) f.Random.Replace("###############"))
                   .RuleFor(Function(a) a.Owners, Function(f) New List(Of Person)(personFaker.GenerateBetween(1,5)))
       accountFaker.Generate().Dump()
    End Sub
    
    Class Person
       Property ID As String = Nothing
       Property Firstname As String = ""
       Property Lastname As String = ""
    End Class
    
    Class Account
       Property AccountNumber As String = ""
       Property Owners As New List(Of Person)
    End Class
    

    【讨论】:

      猜你喜欢
      • 2021-11-29
      • 1970-01-01
      • 1970-01-01
      • 2013-11-27
      • 1970-01-01
      • 2015-08-13
      • 2016-12-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多