Bogus faked strings length in range between values

5k Views Asked by At

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
2

There are 2 best solutions below

0
On BEST ANSWER

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);
0
On

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));