Question

When i set property with "with" method, its leave null all propertys on nested objects which same named.

(im using autofixture's latest version as 3.0.8)

public class Something {
    public string Id { get; set; }
    public IList<Something> Things { get; set; }
}

var obj = Fixture.Build<Something>().With(q => q.Id, "something").CreateAnonymous()

In this situation, obj.Id == "something" equals to true, but also obj.Things[0].Id == null equalsto true.

I think there is a bug or im mistaken; Anyone may help?

Was it helpful?

Solution

By default, AutoFixture will not create an instance of Something because the graph contains a circular reference.

What you can do is to add / remove the appropriate behaviors on the Fixture instance:

fixture.Behaviors.Remove(new ThrowingRecursionBehavior());
fixture.Behaviors.Add(new OmitOnRecursionBehavior());

You can now create an instance of Something however the Things property (circular reference) is now omitted.

That's why you get an empty list..

However, you can customize the creation algorithm even further:

var obj = fixture.Build<Something>()
    .With(x => x.Id, 
        "something")
    .With(x => x.Things, 
        fixture.CreateMany<Something>().ToList())
    .Create();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top