Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Controller asynchronous action isn't correctly evaluated #401

Open
vesselintodorov123 opened this issue Jan 11, 2022 · 0 comments
Open

Controller asynchronous action isn't correctly evaluated #401

vesselintodorov123 opened this issue Jan 11, 2022 · 0 comments
Assignees
Labels

Comments

@vesselintodorov123
Copy link

vesselintodorov123 commented Jan 11, 2022

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.ShouldReturnActionResultTestBuilder1.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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants