Using Bogus to set member property value range

582 views Asked by At

I have the following classes:

  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

Using Bogus, I set a range of values from 1,000 to 10,000 for Person.ID, like so:

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

How do I set Account.Owners to utilize Person.ID values as defined in fakePerson when I instantiate an instance of the Account class like so?:

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

There are 1 answers

0
user3574075 On BEST ANSWER

Solution provided by Bogus author bchavez at 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