有一個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