C
C#3mo ago
Denis

Bogus hierarchical data

Given I have the following class definition
public sealed class Project
{
/// <summary>
/// Unique project identifier
/// </summary>
public required Guid Id { get; init; }

/// <summary>
/// Project name
/// </summary>
public required string Name { get; init; }

/// <summary>
/// Project parent
/// </summary>
public Project? Parent { get; init; }

/// <summary>
/// Sub-projects
/// </summary>
public IReadOnlyCollection<Project> SubProjects { get; init; } = Array.Empty<Project>();
}
public sealed class Project
{
/// <summary>
/// Unique project identifier
/// </summary>
public required Guid Id { get; init; }

/// <summary>
/// Project name
/// </summary>
public required string Name { get; init; }

/// <summary>
/// Project parent
/// </summary>
public Project? Parent { get; init; }

/// <summary>
/// Sub-projects
/// </summary>
public IReadOnlyCollection<Project> SubProjects { get; init; } = Array.Empty<Project>();
}
I wish to generate test instances of Project using Bogus (https://github.com/bchavez/Bogus). So, I define a faker like so:
var projectFaker = new Faker<Project>()
.RuleFor(document => document.Id, faker => faker.Random.Guid())
.RuleFor(document => document.Name, faker => faker.System.FileName())
var projectFaker = new Faker<Project>()
.RuleFor(document => document.Id, faker => faker.Random.Guid())
.RuleFor(document => document.Name, faker => faker.System.FileName())
But how do I fake the Parent property and the SubProjects property?
1 Reply
Lisa
Lisa3mo ago
https://github.com/bchavez/Bogus/issues/290 pretty much describes how to solve this issue
GitHub
Hierarchy support? · Issue #290 · bchavez/Bogus
Version Information Software Version(s) Bogus NuGet Package 29.0.2 .NET Core? netstandard2.1 .NET Full Framework? no Windows OS? Windows 10 Linux OS? Visual Studio? 2019 (16.5.2) What locale are yo...