Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autofixture Fixture.Build().With() On Same Property Name

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?

like image 348
Oğuzhan Topçu Avatar asked May 06 '13 07:05

Oğuzhan Topçu


1 Answers

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();
like image 78
Nikos Baxevanis Avatar answered Nov 19 '22 23:11

Nikos Baxevanis