There is a beautiful library that generates random/pseudo-random values for a DTO.
var fruit = new[] { "apple", "banana", "orange", "strawberry", "kiwi" };
var orderIds = 0;
var testOrders = new Faker<Order>()
//Ensure all properties have rules. By default, StrictMode is false
//Set a global policy by using Faker.DefaultStrictMode
.StrictMode(true)
//OrderId is deterministic
.RuleFor(o => o.OrderId, f => orderIds++)
//Pick some fruit from a basket
.RuleFor(o => o.Item, f => f.PickRandom(fruit))
//A random quantity from 1 to 10
.RuleFor(o => o.Quantity, f => f.Random.Number(1, 10));
To create a rule for an int is simple:
.RuleForType(typeof(int), f => f.Random.Number(10, 1000))
How do we create rules for nullable primitive types?
For example if our model has nullable ints or nullable deimcals:
public class ObjectWithNullables
{
public int? mynumber{get;set;}
public decimal? mydec {get;set;}
}
We cannot construct like so:
.RuleForType(typeof(int?), f => f.Random.Number(10, 1000))
How do we represent nullables?
RuleForType
? Have you read the documentation? – Notepaper