-
Notifications
You must be signed in to change notification settings - Fork 178
Description
When trying to check the result of a controller's asynchronous action through the builder's Calling() and ShouldReturn().ActionResult(...) methods I get the following exception:
MyTested.AspNetCore.Mvc.Exceptions.InvocationResultAssertionException
When calling List action in WeatherForecastController expected result to be ActionResult<IEnumerable>, but instead received Task<ActionResult<IEnumerable>>.
at MyTested.AspNetCore.Mvc.Utilities.Validators.InvocationResultValidator.ThrowNewInvocationResultAssertionException(ComponentTestContext testContext, String typeNameOfExpectedReturnValue, String typeNameOfActionResult)
at MyTested.AspNetCore.Mvc.Utilities.Validators.InvocationResultValidator.ValidateInvocationResultType(ComponentTestContext testContext, Type typeOfExpectedReturnValue, Boolean canBeAssignable, Boolean allowDifferentGenericTypeDefinitions, Type typeOfActualReturnValue)
at MyTested.AspNetCore.Mvc.Utilities.Validators.InvocationResultValidator.ValidateInvocationResultType[TExpectedType](ComponentTestContext testContext, Boolean canBeAssignable, Boolean allowDifferentGenericTypeDefinitions, Type typeOfActualReturnValue)
at MyTested.AspNetCore.Mvc.Builders.Actions.ShouldReturn.ShouldReturnActionResultTestBuilder1.ValidateActionResult[TResult]() at MyTested.AspNetCore.Mvc.Builders.Actions.ShouldReturn.ShouldReturnActionResultTestBuilder
1.ActionResult[TResult](Action`1 actionResultTestBuilder)
at EcontHub.Services.Startup.Specs.CategoryControllerSpecs.ListShouldReturnAllCategories()
Afterwards when creating a sample in order to report this I've tried using .ResultOfType<> but then got the following error:
When calling Get action in WeatherForecastController expected result to be Task<ActionResult<IEnumerable>>, but instead received ObjectResult.
That got me thinking of using ObjectResult and came up with a solution of expecting an ObjectResult and then casting it. It works but it's not perfect. Another person suggested using ShouldReturn().Object(result => result.WithModelOfType().Passing(model => model.Should().NotBeNull())) so I've added that solution to the sample project as well as it seems like the best yet.
Project files
MyTestedAspNetEvaluationError.zip
Startup classes
Present in the attachment.
Expected behavior
I should be able to directly use ShouldReturn().ActionResult<...>(...)
Environment:
- OS: macOS Monterey
- ASP.NET Core Version 5.0