我可以伪造国家吗(C#)

Can I Set the Country in Bogus (C#)

我刚刚开始使用 C# .net 5.0 中的 Bogus。 我正在设法 return 沙盒应用程序中非常有用的数据,但我想将数据限制为基于美国。有没有办法做到这一点? (这是我的沙盒应用程序的一部分)

using Bogus;

namespace FrankenPeople
{
    public class GetBogus
    {
        public enum Gender
        {
            Male,
            Female
        }

        private static int userId = 1;

        private static readonly Faker<Person> fakeData = new Faker<Person>()
            .RuleFor(p => p.Id, f => userId++)
            .RuleFor(p => p.Gender, f => f.PickRandom<Gender>().ToString())
            .RuleFor(p => p.Title, f => f.Name.Prefix(f.Person.Gender))
            .RuleFor(p => p.FirstName, f => f.Name.FirstName(f.Person.Gender))
            .RuleFor(p => p.MiddleName, f => f.Name.FirstName(f.Person.Gender))
            .RuleFor(p => p.LastName, f => f.Name.LastName(f.Person.Gender))
            .RuleFor(p => p.StreetAddress, f => f.Address.StreetAddress())
            .RuleFor(p => p.StreetName, f => f.Address.StreetName())
            .RuleFor(p => p.City, f => f.Address.City())
            .RuleFor(p => p.State, f => f.Address.State())
            .RuleFor(p => p.Country, f => f.Address.Country())
            .RuleFor(p => p.ZipCode, f => f.Address.ZipCode())
            .RuleFor(p => p.Phone, f => f.Phone.PhoneNumber("(###)-###-####"))
            .RuleFor(p => p.Email, (f, p) => f.Internet.Email(p.FirstName, p.LastName))
            .RuleFor(p => p.SSN, f => f.Random.Replace("###-##-####"))
            .RuleFor(p => p.DOB, f => f.Date.Past(18))
        ;
        public static Faker<Person> FakeData => fakeData;
    }
}

Using locales 您可以获得地址、域名后缀和 phone 号码:

var faker = new Faker("en_US");

默认情况下,Bogus 使用en 语言环境,该语言环境大致 美国。如果您想尽可能将事情锁定在美国,请使用 en_US 语言环境。

具体来说,在您的代码示例中,要指定默认区域以外的区域设置,请更改:

fakeData = new Faker<Person>()   //default is `en`
             ...

fakeData = new Faker<Person>(locale: "en_US")
             ...

Locales 并非 100% 完美,请务必阅读 README 文件这部分的小注释:https://github.com/bchavez/Bogus#locales

Note: Some locales may not have a complete data set. For example, zh_CN does not have a lorem data set, but ko has a lorem data set. Bogus will default to en if a locale-specific data set is not found. To further illustrate the previous example, the missing zh_CN:lorem data set will default to the en:lorem data set.