문제

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?

도움이 되었습니까?

해결책

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();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top