AutoFixture – ObjectCreationException

Posted on Updated on

Pojawia mi się podczas testów integracyjnych bazy. Korzystam z EntityFramework i mam relacje jeden do wielu. Występuja cykle.

Czyli po wykonaniu:

var account = fixture.Build<Account>();


AutoFixture was unable to create an instance of type Ploeh.AutoFixture.Kernel.FiniteSequenceRequest 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.


// for all fixture, per TYPE
fixture.Register<ExtensionDataObject>(() => null);

// for one object creation, per property
var account = fixture.Build<Account>()
    .Without(a => a.CustomerAccount)

AutoFixture – zmniejszenie użycia ‘new’

Posted on

Obiekty powinny być tworzone w Fabrykach. Innymi słowy, nie używamy ‘new’ bezpośrednio. Podobnie powinno być w UnitTestach.


            var notifications = new[]
                new Notification(),
                new Notification(),
                new Notification(),
                new Notification()

Dostajemy dzięki AutoFixture:

            var fixture = new Fixture();
            var notifications = fixture.CreateMany<Notification>(4);

AutoFixture scenariusze

Posted on Updated on

var twoUsers = fixture.CreateMany<User>(2);