Reputation: 1796
I am creating tests against an Entity Framework 6 repository. Due to a dependency between a product type and loan, I get a circular reference error when using Autofixture.AutoMoq. Is there an attribute that I can place over a test method so I can eliminate the following line of code (and its related items in the example, below): "fixture.Inject(Enumerable.Empty<Loan>());"
I am using XUnit 2.1.0.3179, Autofixture 3.50.2.0, Autofixutre.AutoMoq 3.50.2.0, AutoFixture.Xnit2, Moq 4.5.29.0
Here are some additional details, which may be helpful...
Loan(N) --- (1) ProductType
Due to the circular reference in my EF6 model, I cannot do the following:
[Theory, AutoMoqData]
public void ProductTypes_GetList()
List<ProductType> productTypeList,
[Frozen] Mock<IProductTypeRepository> productTypeRepo)
{
Instead, I have to do the following in order to avoid the circular reference issue:
var fixture = new Fixture().Customize(new AutoMoqCustomization());
// Avoid circular dependency in EF.
// Eliminating the many side of the relationship.
fixture.Inject(Enumerable.Empty<Loan>());
var productTypeRepo = fixture.Freeze<Mock<IProductTypeRepository>>();
// Create a list of product types.
List<ProductType> productTypeList = fixture.Create<List<ProductType>>();
productTypeRepo.Setup(_ => _.GetAll()).Returns(productTypeList);
I would appreciate learning whether it is possible to achieve my goal and reduce the lines of code.
Thank you in advance for time and suggestions.
Mike
Upvotes: 3
Views: 1120
Reputation: 4319
You can use customization to do this, and wrap your own customization into an attribute..
Heres an example of a customization:
fixture.Customize<ProductType>(x => x.Without(y => y.ProductTypes));
You can wrap customizations up into a class that implements ICustomization
and finally following the guidance in Encapsulating AutoFixture Customizations by Mark Seemann, you can wrap that all up nicely into an attribute so you can do:
[Theory, AutoMoqData, MyCustomizations]
public void ProductTypes_GetList()
Upvotes: 2