Search code examples
c#.netbogus

how to represent nullable values with bogus?


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?


Solution

  • A quick perusal seems to indicate you only need to use RuleForType when you are attempting to provide a single rule for all field/properties of a given type.

    I think your issue with RuleForType is you did not pass in a lambda that returned the correct type. The type as the first parameter must match the return type of the lambda. Use

    .RuleForType(typeof(int?), f => (int?)f.Random.Number(10, 1000))
    

    If you need some possibility of null values, choose a percentage and return null occasionally:

    .RuleForType(typeof(int?), f => (f.Random.Number(1,10) == 1 ? (int?)null : f.Random.Number(10, 1000)))