ivaylokenov/MyTested.AspNetCore.Mvc

When calling action expected result to be OkResult, but instead received T

TonyTroeff opened this issue · 2 comments

Describe your issue
My controller method returns ActionResult<UserConstraintsViewModel>.
This is my method:
image

This is my test that validates this simple action:
image

However, it does not pass. The exception message says: "MyTested.AspNetCore.Mvc.Exceptions.InvocationResultAssertionException : When calling UserConstraints action in ConstraintsController expected result to be OkResult, but instead received UserConstraintsViewModel."

Expected behavior
According to your documentation it is written correctly, so it is reasonable to pass.

Startup classes
My TestStartup class simply inherits StartUp. No additional configuratios have been made.

Environment
Windows 10
ASP.NET Core 3.1

@TonyTroeff, you can use .ShouldReturn().Result(expectedModel) with ActionResult<T> return types.

I will take a look to see how I can improve the error message to be friendlier.

Checked it in the source code - I will consider it as a bug since the current implementation is very confusing. Will add better support for your request and will deploy a fix as soon as possible.

@TonyTroeff I just published a new version, fixing this - 3.1.1. It is available on NuGet. Additionally, I added a new assertion chain, called .ActionResult<TResult>(), which allows to test ActionResult<T> easily. I will close the issue once I merge the code.