C#C
C#2y ago
1 reply
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>();
}


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())

But how do I fake the Parent property and the SubProjects property?
Was this page helpful?