Skip to content

Instantly share code, notes, and snippets.

@tocalai
Created August 8, 2019 02:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tocalai/1f36213a3d7d05ed1c23e0a30c3861e2 to your computer and use it in GitHub Desktop.
Save tocalai/1f36213a3d7d05ed1c23e0a30c3861e2 to your computer and use it in GitHub Desktop.
Demonstrate how to generate mock/testing data via BOGUS
using Newtonsoft.Json;
using System;
using System.Linq;
namespace Bogus.Lab.ConsoleApp
{
class Program
{
static void Main(string[] args)
{
// ensure generate same data for mutiple trigger
Randomizer.Seed = new Random(112233);
// set loccale to english
var users = new Faker<Member>("en")
.RuleFor(u => u.UserId, f => f.Random.Guid())
.RuleFor(u => u.Gender, (f, u) => f.PickRandom<DataSets.Name.Gender>())
.RuleFor(u => u.FirstName, (f, u) => f.Name.FirstName(u.Gender))
.RuleFor(u => u.LastName, (f, u) => f.Name.LastName(u.Gender))
.RuleFor(u => u.DateOfBirth, f => f.Date.Past(10))
// phone number format for japan area
.RuleFor(u => u.Phone, f => f.Phone.PhoneNumber("0#0-####-####"))
.RuleFor(u => u.Type, f => f.PickRandom(Enum.GetNames(typeof(ContactType)).ToArray()))
// amount of fake records
.Generate(10)
;
// print all data to console
Console.WriteLine($"{JsonConvert.SerializeObject(users, Formatting.Indented)}");
}
}
public class Member
{
public Guid UserId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime DateOfBirth { get; set; }
public string Phone { get; set; }
public DataSets.Name.Gender Gender { get; set; }
public string Type { get; set; }
}
public enum ContactType
{
Personal,
Business,
Faimly,
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment