Autofixture: dotnet core 3.0 - Unable to create an instance from Microsoft.AspNetCore.Mvc.ModelBinding.BindingInfo

Created on 26 Sep 2019  ·  6Comments  ·  Source: AutoFixture/AutoFixture

I am currently in the process of upgrading a simple project to dotnet core 3. This project has a number of unit tests, the majority against the controller/api layer using AutoFixture & xunit2.

After the upgrade everything is working EXCEPT the autofixture unit tests that attempt to create a controller. They fail with the following exception:

AutoFixture.ObjectCreationExceptionWithPath : AutoFixture was unable to create an instance from Microsoft.AspNetCore.Mvc.ModelBinding.BindingInfo because creation unexpectedly failed with exception. Please refer to the inner exception to investigate the root cause of the failure.

Request path:
*.Inventory.Api.Controllers.ProductCategoriesController controller
*
.Inventory.Api.Controllers.ProductCategoriesController
Microsoft.AspNetCore.Mvc.ControllerContext ControllerContext
Microsoft.AspNetCore.Mvc.ControllerContext
Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor ActionDescriptor
Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor
System.Collections.Generic.IList1[Microsoft.AspNetCore.Mvc.Abstractions.ParameterDescriptor] Parameters System.Collections.Generic.IList1[Microsoft.AspNetCore.Mvc.Abstractions.ParameterDescriptor]
System.Collections.Generic.List1[Microsoft.AspNetCore.Mvc.Abstractions.ParameterDescriptor] System.Collections.Generic.IEnumerable1[Microsoft.AspNetCore.Mvc.Abstractions.ParameterDescriptor] collection
System.Collections.Generic.IEnumerable`1[Microsoft.AspNetCore.Mvc.Abstractions.ParameterDescriptor]
Microsoft.AspNetCore.Mvc.Abstractions.ParameterDescriptor
Microsoft.AspNetCore.Mvc.ModelBinding.BindingInfo BindingInfo
Microsoft.AspNetCore.Mvc.ModelBinding.BindingInfo

Inner exception messages:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
System.ArgumentException: The type 'System.Object' must implement 'Microsoft.AspNetCore.Mvc.ModelBinding.IModelBinder' to be used as a model binder. (Parameter 'value')

Most helpful comment

I know this is closed but for other folks who run into this who _arent_ using Xunit the fast (lazy) workaround is to do

Fixture.Customize<BindingInfo>(c => c.OmitAutoProperties()); -- I'm sure there is a way to _also_ fix bindingInfo if you need it but if you dont in your tests...

All 6 comments

@TroyHouston I've ran into this issue as well. I've fixed it by using the [Greedy] Attribute on my controller.

I'm using Nunit :)

@Fjsmoreira Thanks! That also works for me :)

I know this issue is closed, but I need some help understanding where the [Greedy] Attribute comes from. @Fjsmoreira or @TroyHouston, what package/library do I need to use to get the [Greedy] Attribute?

I know this issue is closed, but I need some help understanding where the [Greedy] Attribute comes from. @Fjsmoreira or @TroyHouston, what package/library do I need to use to get the [Greedy] Attribute?

Never mind. Figured it out! Looks like it is part of the AutoFixture.Xunit package.

I know this is closed but for other folks who run into this who _arent_ using Xunit the fast (lazy) workaround is to do

Fixture.Customize<BindingInfo>(c => c.OmitAutoProperties()); -- I'm sure there is a way to _also_ fix bindingInfo if you need it but if you dont in your tests...

Solved the problem with OmitAutoProperties:
var controller = fixture.Build<SomeController>().OmitAutoProperties().Create();

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zvirja picture zvirja  ·  4Comments

josh-degraw picture josh-degraw  ·  4Comments

joelleortiz picture joelleortiz  ·  4Comments

JoshKeegan picture JoshKeegan  ·  6Comments

ploeh picture ploeh  ·  7Comments