C# 使用Bogus创建模拟数据

很多时候测试方法的时候需要创建一些测试数据,平时我都是自己手写的,

今天发现了一个可以创建模拟数据的轮子Bogus,下面看看怎么使用的

首先引用Nuget包 

github : bchavez/Bogus: A simple fake data generator for C#, F#, and VB.NET. Based on and ported from the famed faker.js. (github.com)

测试代码在这里,更多用法可以看Git的案例

using Bogus;
using Bogus.DataSets;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Bogus.DataSets.Name;

namespace BogusTest
{
    public enum Gender
    {
        Male,

        Female
    }

    public class Person
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public int Age { get; set; }

        public Gender Gender { get; set; }

        public Sect Sect { get; set; }

        public override string ToString()
        {
            return $"{Id}  {Name}  {Age}  {Gender}  {Sect.Name}";
        }
    }

    public class Sect : IEquatable<Sect>
    {
        public string Address { get; set; }

        public string Name { get; set; }

        public DateTime Create { get; set; }

        public string Skill { get; set; }

        public bool Equals(Sect other)
        {
            return other.Name == Name;
        }

        public override string ToString()
        {
            return $"{Name}  {Address}  {Skill}  {Create.ToString("yyyy:MM")}";
        }

        public override bool Equals(object obj)
        {
            if (obj is Sect sect)
            {
                return sect.Equals(this);
            }
            return false;
        }

        public override int GetHashCode()
        {
            return Name.GetHashCode();
        }
    }

    internal class Program
    {
        private static void Main(string[] args)
        {
            Randomizer.Seed = new Random(123);

            //门派信息
            var sectGroups = new List<(string Name, string Skill, string Address, DateTime Create)>()
            {
                ("华山派","华山剑法","华山",new DateTime(1123,6,15)),
                ("古墓派","玉女剑法","古墓",new DateTime(1206,7,25)),
                ("少林派","易筋经","嵩山",new DateTime(1110,2,14)),
                ("丐帮","打狗棒法","",new DateTime(1230,1,21)),
                ("武当派","太极剑","武当山",new DateTime(1005,2,15)),
                ("逍遥派","北冥神功","凌波洞",new DateTime(1000,3,12)),
                ("日月神教","葵花宝典","黑木崖",new DateTime(1109,4,11)),
            };

            var sects = new Faker<Sect>()
                .CustomInstantiator(f =>
                {
                    var group = f.PickRandom(sectGroups);
                    var s = new Sect();
                    s.Name = group.Name;
                    s.Address = group.Address;
                    s.Skill = group.Skill;
                    s.Create = group.Create;
                    return s;
                })//自定义复杂类的生成
                .Generate(15);
            sects.ForEach(s => Console.WriteLine($"{s}"));

            Console.WriteLine("=======================");

            var person = new Faker<Person>()
                .StrictMode(true)//需要为所有的属性设定规则
                .RuleFor(o => o.Id, f => f.Random.Number(0, 10000))//设置Id
                .RuleFor(o => o.Name, (f, o) => f.Name.FirstName())//使用Bogus自带的名称
                .RuleFor(o => o.Gender, f => f.PickRandom<Gender>())//从枚举中随机选择
                .RuleFor(o => o.Age, f => f.Random.Number(18, 54))//设定年龄
                .RuleFor(o => o.Sect, f => f.PickRandom(sects))//从集合中随机选择
                .Generate(50);

            person.ForEach(p => Console.WriteLine($"{p}"));
        }
    }
}

 

posted @ 2022-12-12 09:58  只吃肉不喝酒  阅读(401)  评论(0编辑  收藏  举报