Use AutoFixture to CreateMany of class type with collection

I working on .NET CORE project and using xUnit with Fixture and Moq. I have a class with a collection. When I use autoFixture to create many instances I get an error. If I remove collection ‘SchoolBranches’ from SchoolDataView then it works

Test Class

public void GetAllSchool_ShouldReturn_IQueryableOfSchoolDataView()
        var fixture = new Fixture();

        var schoolDataViewMock = fixture.CreateMany<SchoolDataView>(); // GET ERROR HERE

        var actualSchoolDataList = sut.GetAllSchools();



public class SchoolDataView
    public SchoolDataView()


    public Guid SchoolID { get; set; }
    public string Name { get; set; }

    public Collection<SchoolBranchDataView> SchoolBranches { get; set; }


public class SchoolBranchDataView
    public Guid SchoolBranchID { get; set; }
    public Guid SchoolID { get; set; }
    public string Address { get; set; }
    public int PhoneNumber { get; set; }
    public School School { get; set; }


AutoFixture.ObjectCreationExceptionWithPath: 'AutoFixture was unable to create an instance of type System.Reflection.RuntimePropertyInfo because the traversed object graph contains a circular reference. Information about the circular path follows below. This is the correct behavior when a Fixture is equipped with a ThrowingRecursionBehavior, which is the default. This ensures that you are being made aware of circular references in your code. Your first reaction should be to redesign your API in order to get rid of all circular references. However, if this is not possible (most likely because parts or all of the API is delivered by a third party), you can replace this default behavior with a different behavior: on the Fixture instance, remove the ThrowingRecursionBehavior from Fixture.Behaviors, and instead add an instance of OmitOnRecursionBehavior:

.ForEach(b => fixture.Behaviors.Remove(b));
fixture.Behaviors.Add(new OmitOnRecursionBehavior());

%d bloggers like this: