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>();

Dostaję:

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.

Rozwiązanie:

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

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s