Having the code below, I'm generating 2 student objects using CreateMany(2). Country must be "England" for both objects. The other SchoolId must be unique.
But the students variable below contains School objects with null as SchoolId, while I expect unique values there. What am I doing wrong? Or am I missing some extra configuration?
using AutoFixture;
public class Program
{
public static void Main()
{
var fixture = new Fixture();
var students = fixture
.Build<Student>()
.With(st => st.School, new School
{
//SchoolId = Expecting AutoFixture would generate a unique SchoolId here
Country = "England"
})
.CreateMany(2).ToList();
foreach (var schoolId in students.Select(x => x.School.SchoolId))
{
Console.WriteLine($"schoolId: {schoolId ?? "null"}");
}
}
}
public class Student
{
public string StudentId { get; set; }
public string StudentName { get; set; }
public School School { get; set; }
}
public class School
{
public string SchoolId { get; set; }
public string Country { get; set; }
}
In your
foreachyou are selectingSchoolId, but you haven't specified whatAutoFixtureshould use for it so it's justnull. You can do the below: