Setting up test relations

I want to create some dummy data for my database for integration testing. Im not sure how to do relations right. This is my user entity. I create some dummy users and dogs with bogus but how do i actually link them?
public sealed class User : IdentityUser<int>
{
public string? Bio { get; set; } = string.Empty;
public int Age { get; set; } = 0;

public int ProfilePictureId { get; set; } = 0;
public string ProfilePictureUrl { get; set; } = string.Empty;

public ICollection<Dog> Dogs { get; set; } = [];
public sealed class User : IdentityUser<int>
{
public string? Bio { get; set; } = string.Empty;
public int Age { get; set; } = 0;

public int ProfilePictureId { get; set; } = 0;
public string ProfilePictureUrl { get; set; } = string.Empty;

public ICollection<Dog> Dogs { get; set; } = [];
Here is my dog generation method:
public void GenerateDogs(int count)
{
int idx = 1000;
var dogFaker = new Faker<Dog>()
.RuleFor(d => d.Id, f => idx += 1)
.RuleFor(d => d.Name, f => f.Name.FirstName())
.RuleFor(d => d.Breed, f => f.PickRandom(new[] { "Beagle", "Bulldog", "Poodle", "German Shepherd" }))
.RuleFor(d => d.DateOfBirth, f => f.Date.Past(10).ToUniversalTime().ToDateTimeOffset())
.RuleFor(d => d.Color, f => f.Commerce.Color())
.RuleFor(d => d.IsNeutered, f => f.Random.Bool(0.7f))
.RuleFor(d => d.Notes, f => f.Lorem.Sentence())
.RuleFor(d => d.UserId, f => f.PickRandom(_users).Id)

_dogs = dogFaker.Generate(count);
}
public void GenerateDogs(int count)
{
int idx = 1000;
var dogFaker = new Faker<Dog>()
.RuleFor(d => d.Id, f => idx += 1)
.RuleFor(d => d.Name, f => f.Name.FirstName())
.RuleFor(d => d.Breed, f => f.PickRandom(new[] { "Beagle", "Bulldog", "Poodle", "German Shepherd" }))
.RuleFor(d => d.DateOfBirth, f => f.Date.Past(10).ToUniversalTime().ToDateTimeOffset())
.RuleFor(d => d.Color, f => f.Commerce.Color())
.RuleFor(d => d.IsNeutered, f => f.Random.Bool(0.7f))
.RuleFor(d => d.Notes, f => f.Lorem.Sentence())
.RuleFor(d => d.UserId, f => f.PickRandom(_users).Id)

_dogs = dogFaker.Generate(count);
}
1 Reply
Angius
Angius4d ago
You can use the dogs generator to generate the dogs for the user Or you can do it in two steps: generate users, add them to the database which will give you their IDs, then use a random user ID for the dog generator

Did you find this page helpful?