首页 > 解决方案 > 值之间范围内的伪造字符串长度

问题描述

我为 Bogus 生成的字符串属性编写规则:

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

如何Bogus在指定值之间生成字符串属性?就像是:

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

标签: c#fakerbogus

解决方案


您还可以将.ClampLength(min, max)扩展方法用于任何string. 例如:

using Bogus.Extensions;

.RuleFor(x => x.Name, f => f.Company.CompanyName().ClampLength(1, 30));

推荐阅读