34
loading...
This website collects cookies to deliver better user experience
dotnet add package Bogus
or in Package Manager Console - Install-Package Bogus
and you install the latest version.Car
modelpublic class Car
{
public string VinCode { get; set; }
public string RegistrationNumber { get; set; }
public ColorEnum Color { get; set; }
public DateTime Year { get; set; }
public double Price { get; set; }
public double EngineVolume { get; set; }
public VehicleTypeEnum VehicleType { get; set; }
public BrandEnum Brand { get; set; }
public bool IsAllWheelDrive { get; set; }
public Tire Tire { get; set; }
public Transmission Transmission { get; set; }
}
FakeDataMapper
- mapper for properties, because each property should have own rule to create fake data.// Default example of mapping rules for Car model
public static Faker<Car> DeafultCar = new Faker<Car>()
.RuleFor(x => x.Brand, f => f.PickRandom<BrandEnum>())
.RuleFor(x => x.Color, f => f.Random.Enum<ColorEnum>())
.RuleFor(x => x.EngineVolume, f => f.Random.Double(0, 5))
.RuleFor(x => x.IsAllWheelDrive, f => f.Random.Bool())
.RuleFor(x => x.Price, f => f.Random.Double(1000, 10000))
.RuleFor(x => x.VinCode, f => f.Vehicle.Vin())
.RuleFor(x => x.VehicleType, f => f.Random.Enum<VehicleTypeEnum>())
.RuleFor(x => x.Year, f => f.Date.Past())
.RuleFor(x => x.RegistrationNumber, f => f.Random.Guid().ToString());
f.Vehicle.Vin()
- get string VinCodeRandom
API which provide a lot of methodsenum
you can use two ways to get random value from enum - f.PickRandom<BrandEnum>()
or f.Random.Enum<ColorEnum>()
public static Faker<Car> AdvancedCar = new Faker<Car>()
// Ensure all properties have rules. By default, StrictMode is false
// Example with StrictMode = true
.StrictMode(true)
.RuleFor(x => x.VinCode, f => f.Vehicle.Vin())
.RuleFor(x => x.RegistrationNumber, f => f.Random.Guid().ToString())
// Method to pick random data of your class
.RuleFor(x => x.Brand, f => f.PickRandom<BrandEnum>())
.RuleFor(x => x.Color, f => f.Random.Enum<ColorEnum>())
.RuleFor(x => x.EngineVolume, f => f.Random.Double(0, 5))
.RuleFor(x => x.IsAllWheelDrive, f => f.Random.Bool())
.RuleFor(x => x.Price, f => f.Random.Double(1000, 10000))
// Method to pick random data of your class
.RuleFor(x => x.VehicleType, f => f.Random.Enum<VehicleTypeEnum>())
.RuleFor(x => x.Year, f => f.Date.Past())
.RuleFor(x => x.Tire, f => new Tire
{
Brand = f.PickRandom<TireBrandEnum>(),
Diameter = f.Random.Number(10, 100)
})
.RuleFor(x => x.Transmission, f => new Transmission
{
Brand = f.Company.CompanyName(),
Number = f.Commerce.ProductName()
})
// Optional: After all rules are applied finish with the following action
// Sometimes can be logging or other helpful logic
.FinishWith((faker, car) =>
{
Console.WriteLine($"New car {car.VinCode} was generated successfully");
});
StrictMode(true)
- is checked all properties have their rulesFinishWith
- sometimes can be done after generation dataLocale
property.public static List<Car> GenerateAdvancedCar(int count,
string locale = "en")
{
var generator = FakeDataMapper.AdvancedCar;
generator.Locale = locale;
// Check configuration
generator.AssertConfigurationIsValid();
return generator.Generate(count);
}
[
{
"VinCode":"77BS77F3WSR911082",
"RegistrationNumber":"2e09f4a8-61ca-448e-4b93-4e36f700c418",
"Color":1,
"Year":"2021-10-06T03:36:00.5403703+03:00",
"Price":9891.462153238925,
"EngineVolume":4.046757858734,
"VehicleType":2,
"Brand":22,
"IsAllWheelDrive":false,
"Tire":{
"Brand":6,
"Width":0.0,
"Profile":0.0,
"Diameter":70
},
"Transmission":{
"Brand":"Wilkinson - Fritsch",
"Number":"Handcrafted Metal Bacon"
}
}
]
private static IEnumerable<TestCaseData> Data
{
get
{
yield return new TestCaseData(100);
yield return new TestCaseData(1000);
yield return new TestCaseData(10000);
yield return new TestCaseData(100000);
}
}
[Test]
public void TestAssertConfiguration_ShouldSuccess()
{
var generator = new FakeGenerator(1, "en");
generator.AssertConfiguration();
}
[Test]
[TestCaseSource(nameof(Data))]
public void GenerateSimpleCar_ShouldSuccess(int count)
{
var generator = new FakeGenerator(count, "en");
var data = generator.GenerateDefaultCars();
Assert.NotNull(data);
Assert.True(data.Count == count);
}