bchavez / Bogus

:card_index: A simple fake data generator for C#, F#, and VB.NET. Based on and ported from the famed faker.js.
Other
8.66k stars 495 forks source link

Set collection member property value range #394

Closed taiwoa closed 2 years ago

taiwoa commented 2 years ago

I am using Bogus version 33.1.1.0 in Visual Studio 2019 with VB.NET.

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

How do I use Rules or RuleFor to set a range of values between 1,000 to 10,000 for Person.ID when I instantiate an instance of the Account class like so:

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

I tried the following and got the exception: "Overload resolution failed because no accessible 'RuleFor' can be called with these arguments":

    Dim fp = New Faker(Of Person)().StrictMode(False).Rules(
      Sub(c, p)
        p.ID = c.Random.Int(1000, 10000).ToString
      End Sub
      )
    Dim fps = New List(Of Faker(Of Person)) From {fp}

    Dim af = New Faker(Of Account)().
      RuleFor(Function(a) a.Owners, Function(f) fps)

Thank you.

bchavez commented 2 years ago

The following seems to work fine:

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

image