3

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
michasaucer
  • 4,562
  • 9
  • 40
  • 91

2 Answers2

8

You can also use the .ClampLength(min, max) extension method for any string. Eg:

using Bogus.Extensions;

.RuleFor(x => x.Name, f => f.Company.CompanyName().ClampLength(1, 30));
Brian Chavez
  • 8,048
  • 5
  • 54
  • 47
5

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);
zhulien
  • 5,145
  • 3
  • 22
  • 36