Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# library to populate object with random data [closed]

Tags:

c#

I want to populate my object with random data (for testing purposes), is there a library to do it?

Some kind of reflection method that will traverse object graph and initialize primitive properties like (string, int, DateTime, etc) (but do it the deep way, including collections, child objects, etc)

like image 242
Alex Burtsev Avatar asked Jul 08 '11 13:07

Alex Burtsev


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

Bogus

Bogus is a simple and sane fake data generator for C# and .NET. A C# port of faker.js and inspired by FluentValidation's syntax sugar. Supports .NET Core.

Setup

public enum Gender {    Male,    Female }  var userIds = 0;  var testUsers = new Faker<User>()     //Optional: Call for objects that have complex initialization     .CustomInstantiator(f => new User(userIds++, f.Random.Replace("###-##-####")))      //Basic rules using built-in generators     .RuleFor(u => u.FirstName, f => f.Name.FirstName())     .RuleFor(u => u.LastName, f => f.Name.LastName())     .RuleFor(u => u.Avatar, f => f.Internet.Avatar())     .RuleFor(u => u.UserName, (f, u) => f.Internet.UserName(u.FirstName, u.LastName))     .RuleFor(u => u.Email, (f, u) => f.Internet.Email(u.FirstName, u.LastName))     //Use an enum outside scope.     .RuleFor(u => u.Gender, f => f.PickRandom<Gender>())     //Use a method outside scope.     .RuleFor(u => u.CartId, f => Guid.NewGuid()); 

Generate

var user = testUsers.Generate(); Console.WriteLine(user.DumpAsJson());  /* OUTPUT: {   "Id": 0,   "FirstName": "Audrey",   "LastName": "Spencer",   "FullName": "Audrey Spencer",   "UserName": "Audrey_Spencer72",   "Email": "[email protected]",   "Avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/itstotallyamy/128.jpg",   "Gender": 0,   "CartId": "863f9462-5b88-471f-b833-991d68db8c93", .... 

Without Fluent Syntax

  public void Without_Fluent_Syntax()   {       var random = new Bogus.Randomizer();       var lorem = new Bogus.DataSets.Lorem();       var o = new Order()           {               OrderId = random.Number(1, 100),               Item = lorem.Sentence(),               Quantity = random.Number(1, 10)           };       o.Dump();   }   /* OUTPUT:   {     "OrderId": 61,     "Item": "vel est ipsa",     "Quantity": 7   } */ 
like image 121
Brian Chavez Avatar answered Sep 30 '22 10:09

Brian Chavez