有一个beautiful library为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));
要创建一个整型的规则很简单:
.RuleForType(typeof(int), f => f.Random.Number(10, 1000))
我们如何创建为空的原始类型的规则?
例如,如果我们的模型具有可空整数或为空的deimcals:
public class ObjectWithNullables
{
public int? mynumber{get;set;}
public decimal? mydec {get;set;}
}
我们不能建立像这样:
.RuleForType(typeof(int?), f => f.Random.Number(10, 1000))
我们如何代表nullables?
会发生什么事,当你尝试你的榜样'RuleForType'?您是否阅读过文档? – NetMage