有時候在做 POC 或是要放入測試資料,最煩的不是功能,而是資料最常就是 for 1~1000 之類的
之前就知道有一套叫做 Bogus 的 library 可以幫忙生成但是之前記得就是都是英文
最近看一下一些 open source 專案又看到他一下,發現他可以產生中文了,還是我當初漏看這地方我也不知道,現在用起來舒服多了
這邊就是紀錄一下自己常用到的一些類型
物件模型:
public class SampleData
{
public Guid Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public bool IsActive { get; set; }
public int Age { get; set; }
public decimal Salary { get; set; }
public DateTime Create { get; set; }
public string CheineseName { get; set; }
public string Addr { get; set; }
}
產生測試資料:
var fakerZh = new Faker("zh_TW"); // 中文
var faker = new Faker("en")
.RuleFor(x => x.Id, f => Guid.NewGuid()) // Guid
.RuleFor(x => x.Name, f => f.Name.FullName()) // string
.RuleFor(u => u.Email, (f, u) => f.Internet.Email(u.Name)) //Email
.RuleFor(x => x.IsActive, f => f.Random.Bool()) // bool
.RuleFor(x => x.Age, f => f.Random.Int(18, 80)) // int
.RuleFor(x => x.Salary, f => f.Random.Decimal(min: 1.00m, max: 1000.00m)) // decimal
.RuleFor(x => x.Create, f => f.Date.Past(2)) //Datetime
.RuleFor(x => x.Addr, f => fakerZh.Address.FullAddress()) // 中文地址
.RuleFor(x => x.CheineseName, f => fakerZh.Name.LastName()+ fakerZh.Name.FirstName());// 中文名稱 中文排版問題,先是FirstName+LastName
var data = faker.Generate(5);
foreach (var item in data)
{
Console.WriteLine("[" + item.Create + "] " + item.Name + "(" + item.Id + ")");
Console.WriteLine("中文名稱:"+item.CheineseName+",地址:"+item.Addr);
Console.WriteLine("年齡:" + item.Age + ",Active" + item.IsActive + ",薪水:" + item.Salary.ToString("F") + ",Email:" + item.Email);
Console.WriteLine();
}
result:
[2024/6/3 下午 09:03:12] Deshawn Von(017fcc06-494f-4dd5-99d0-7de4b17f452d)
中文名稱:夏雨澤,地址:陶路79號, 苗栗市, Saint Lucia
年齡:40,ActiveTrue,薪水:47.29,Email:DeshawnVon32@gmail.com
[2024/3/13 上午 12:42:40] Douglas Schmeler(321e25cb-4de8-4f97-ba6c-b1e6b05fa9a5)
中文名稱:龍靖琪,地址:楊街67號, 花蓮縣, Tonga
年齡:20,ActiveFalse,薪水:445.51,Email:DouglasSchmeler.Pfeffer@gmail.com
[2025/6/9 上午 12:31:30] Calista McGlynn(9db4ceff-9eb4-41ef-a87a-28a7d31873a5)
中文名稱:毛鵬濤,地址:覃路8542號, 臺東縣, Saint Pierre and Miquelon
年齡:37,ActiveTrue,薪水:546.45,Email:CalistaMcGlynn32@gmail.com
[2024/8/25 上午 10:08:31] Daron Stark(17f13f6e-d56b-4ec8-8c32-3d54c393753c)
中文名稱:夏鶴軒,地址:邱西路93號, 彰化縣, Bouvet Island (Bouvetoya)
年齡:76,ActiveTrue,薪水:480.42,Email:DaronStark22@yahoo.com
[2024/12/23 上午 05:30:46] Rylan Reichert(ac3e44f2-1c78-40f6-9fed-1e3ae41ed1ec)
中文名稱:賴昊天,地址:郭北路0267號, 臺中市, Wallis and Futuna
年齡:49,ActiveFalse,薪水:928.05,Email:RylanReichert43@hotmail.com
小結論,其實放測試的假資料其實很方便,中文地址有點太假了,後來看其他文件他是可以擴充的
但是畢竟圖個方便,如果要擴充就麻煩了,不過 Bogus 是真的蠻方便的
reference:
https://github.com/bchavez/Bogus
擴充推薦看這:
https://ithelp.ithome.com.tw/articles/10375501