Search code examples
c#fakerbogus

Bogus faked strings length in range between values


Im writing rule for my string property generated by Bogus:

var fakeThings= new Faker<Thing>()
   .RuleFor(x => x.Name, f => f.Company.CompanyName());

How to generate string property in Bogus between specified values? Something like:

.RuleFor(x => x.Name, f => f.Company.CompanyName().Length(1, 30);
// returns CompanyName with min 1 char and max 30 chars

Solution

  • It depends on which version you are using, really. I would suggest you try String2 or Utf16String depending on your desired character set.

    .RuleFor(x => x.Name, f => f.Random.String2(1, 30);