其他分享
首页 > 其他分享> > 不一样的虚假(Bogus)和骗子(Faker)

不一样的虚假(Bogus)和骗子(Faker)

作者:互联网

  首先声明,Bogus是一个Nuget包的名称,Faker是它内部的一个类,这个库的作用就是模拟生成实体类的数据,特别适合测试时的场景,不用苦思冥想,辛辛苦苦来造不同的实体类了。当然,造出来的都是虚假的(Bogus),骗人的(Faker)。它家在这里:https://github.com/bchavez/Bogus

  假如我有个会员类

   enum Sex
    {
        男,
        女
    }
    class Memmber
    {
        public Guid ID { get; set; }
        public long NO { get; set; }
        public string Name { get; set; }
        public Sex Sex { get; set; }
        public int Age { get; set; }
        public string Email { get; set; }
        public string Phone { get; set; }
        public decimal Balance { get; set; }
        public DateTime CreateOn { get; set; }
    }

  这个类在测试时,要手动实例化,穿梭于各个层和方法中间,现在把构建这个类的对象(实例),交给Bogus。

  首先引入Bogus包

Install-Package Bogus

  接下来就可以告诉Faker怎么来“骗人”了

var options = new JsonSerializerOptions();
options.Encoder = System.Text.Encodings.Web.JavaScriptEncoder.Create(UnicodeRanges.All);
while (true)
{
    var personFaker = new Faker<Memmber>()
        .RuleFor(x => x.ID, x => x.Random.Guid())
        .RuleFor(x=>x.NO,x=>x.Random.Long(100000000000,999999999999))
        .RuleFor(x => x.Name, x => x.Person.LastName + x.Person.FirstName)
        .RuleFor(x => x.Sex, x => x.Random.Enum<Sex>())
        .RuleFor(x => x.Email, x => x.Person.Email)
        .RuleFor(x => x.Phone, x => x.Person.Phone)
        .RuleFor(x => x.Age, x => x.Random.Int(0, 120))
        .RuleFor(x=>x.Balance,x=>x.Finance.Amount(0,1000))                    
        .RuleFor(x => x.CreateOn, x => x.Date.Past(1));

    Console.WriteLine(JsonSerializer.Serialize(personFaker.Generate(), options));
    Console.ReadLine();
}

看一下效果:

{
  "ID":"5f3d97f6-52da-7fb6-e73f-153fd3a41bfb",
  "NO":539140308276,
  "Name":"KoeppErick",
  "Sex":0,
  "Age":48,
  "Email":"Erick.Koepp@hotmail.com",
  "Phone":"(899) 392-6546",
  "Balance":45.34,
  "CreateOn":"2021-02-14T13:32:19.892062+09:00"
}

看来,Faker是通过RuleFor来给每个属性来模拟那种格式的数据的,比如RuleFor(x => x.Email, x => x.Person.Email),告诉Faker,这是一个Email格式,记得骗人的时候,给人家一个email格式的数据,内容随便你,那问题来了,是不是有一大组格式呢?当然,这也是这个库的作用,都有哪些?

Bogus API Support


  是不是挺丰富的,一般场景应该够用了,如果有特殊数据模拟,也只能自已动手了。

 

  对,我是一个祖国的程序员,Member类生成的实例是外国的,这不行,换一下:

  var options = new JsonSerializerOptions();
            options.Encoder = System.Text.Encodings.Web.JavaScriptEncoder.Create(UnicodeRanges.All);
            while (true)
            {
                var personFaker = new Faker<Memmber>("zh_CN")
                    .RuleFor(x => x.ID, x => x.Random.Guid())
                    .RuleFor(x=>x.NO,x=>x.Random.Long(100000000000,999999999999))
                    .RuleFor(x => x.Name, x => x.Person.LastName + x.Person.FirstName)
                    .RuleFor(x => x.Sex, x => x.Random.Enum<Sex>())
                    .RuleFor(x => x.Email, x => x.Person.Email)
                    .RuleFor(x => x.Phone, x => x.Person.Phone)
                    .RuleFor(x => x.Age, x => x.Random.Int(0, 120))
                    .RuleFor(x=>x.Balance,x=>x.Finance.Amount(0,1000))                    
                    .RuleFor(x => x.CreateOn, x => x.Date.Past(1));

                Console.WriteLine(JsonSerializer.Serialize(personFaker.Generate(), options));
                Console.ReadLine();
            }

  只需要在new Faker<T>时告这个骗子是要骗祖国的程序,就可以了,看结果吧:

{
  "ID":"219d56ca-ea21-3a02-84b5-d9950ae8f384",
  "NO":362783628351,
  "Name":"徐思远",
  "Sex":0,
  "Age":100,
  "Email":"40@gmail.com",
  "Phone":"024-65735687",
  "Balance":623.22,
  "CreateOn":"2021-01-19T19:38:30.1504231+09:00"
}

其中的名字还真像个真人名,电话号也换成祖国的了。

那还支持其他国家吗?当然

Locale Code

Language

 

Locale Code

Language

af_ZA

Afrikaans

 

fr_CH

French  (Switzerland)

ar

Arabic

 

ge

Georgian

az

Azerbaijani

 

hr

Hrvatski

cz

Czech

 

id_ID

Indonesia

de

German

 

it

Italian

de_AT

German  (Austria)

 

ja

Japanese

de_CH

German  (Switzerland)

 

ko

Korean

el

Greek

 

lv

Latvian

en

English

 

nb_NO

Norwegian

en_AU

English  (Australia)

 

ne

Nepalese

en_AU_ocker

English  (Australia Ocker)

 

nl

Dutch

en_BORK

English (Bork)

 

nl_BE

Dutch  (Belgium)

en_CA

English  (Canada)

 

pl

Polish

en_GB

English (Great  Britain)

 

pt_BR

Portuguese  (Brazil)

en_IE

English  (Ireland)

 

pt_PT

Portuguese  (Portugal)

en_IND

English  (India)

 

ro

Romanian

en_NG

Nigeria  (English)

 

ru

Russian

en_US

English  (United States)

 

sk

Slovakian

en_ZA

English (South  Africa)

 

sv

Swedish

es

Spanish

 

tr

Turkish

es_MX

Spanish  (Mexico)

 

uk

Ukrainian

fa

Farsi

 

vi

Vietnamese

fi

Finnish

 

zh_CN

Chinese

fr

French

 

zh_TW

Chinese  (Taiwan)

fr_CA

French  (Canada)

 

zu_ZA

Zulu (South  Africa)

 

其实还有Bogus有很多其他功能,大家可以自行探索。

 

  想要更快更方便的了解相关知识,可以关注微信公众号   

 

 

标签:RuleFor,Generates,Get,random,Faker,Bogus,骗子,Gets,name
来源: https://www.cnblogs.com/ljknlb/p/15862473.html