Lesson 57 of 93 · 6 min read · Updated
Verifying the ActionResult Contract: ViewResult, RedirectToAction and NotFound
On this page
An MVC action does not render HTML in a unit test. It returns an IActionResult object, and that object is the contract between the controller and the framework. This lesson shows how to assert that contract with xUnit and FluentAssertions: the result type, the view model, the route values and the status it maps to.
Technical Requirements
- .NET 11 SDK
- xUnit 2.9.3
- Moq 4.21.0
- FluentAssertions 7.2.2 (free under Apache-2.0; version 8.x requires a commercial licence)
- A test project with a
FrameworkReferencetoMicrosoft.AspNetCore.App
What Belongs to the Contract
When a browser calls an action, ASP.NET Core takes the returned result and turns it into a response. A ViewResult renders a Razor view with a model. A RedirectToActionResult becomes a 302 response with a Location header. A NotFoundResult becomes a 404. The unit test stops before that step and checks the result object itself.
That object carries four things worth asserting:
- the result type (
ViewResult,RedirectToActionResult,NotFoundResultand so on), - the view model,
- the route values of a redirect,
- the status code it stands for.
The view's HTML is not part of the contract in a unit test. Razor does not run, so there is nothing to check there. If you need to check rendered markup, that is an integration test.
Result Type and View Model
The Index action loads products and maps them to view models. The test checks both the type of the result and the shape of the model.
M07.WebApp/Controllers/ProductsController.cs
M07.WebApp.Tests/MvcControllerTests.cs
Two FluentAssertions details make this readable. BeOfType<ViewResult>() checks the exact runtime type, and .Subject gives you the value already cast to that type. For the model the test uses BeAssignableTo<IReadOnlyList<ProductViewModel>>() instead, because the action returns a List<T>, and the test should not care which concrete collection it is. Use BeOfType when the exact type is the promise and BeAssignableTo when only the shape is.
The Details action shows the same idea with a single model, chained with .Which:
M07.WebApp.Tests/MvcControllerTests.cs
Route Values Are Part of the Contract
After a successful POST, the controller follows the Post/Redirect/Get pattern: it redirects to another action instead of rendering a view. A redirect to the wrong action, or to the right action with the wrong id, sends the user to the wrong page. So the test checks both ActionName and RouteValues.
M07.WebApp.Tests/MvcControllerTests.cs
The test uses nameof(ProductsController.Details) instead of the string "Details". If someone renames the action, the compiler flags the test, and a typo cannot slip in. The id comes from the repository mock (42), not from the request, so the test proves that the controller redirects to the product that was really created.
Empty and Pre-filled Forms
GET actions that show a form also have a contract: the right model type, empty or filled with current values.
M07.WebApp.Tests/MvcControllerTests.cs
Mock.Of<IProductRepository>() creates a dummy. The Create GET action never touches the repository, so the test does not need any setup.
The Edit GET test checks each field of the form model. This matters because the edit form is the only place where the user sees the current values. If the mapping copied the wrong field, or left one out, the user would save stale or empty data on the next POST. Three short assertions protect against that.
NotFound: the Path People Forget
A missing resource must return NotFoundResult, not a ViewResult with a null model and not an exception. The Delete action relies on the repository's bool result to decide.
M07.WebApp/Controllers/ProductsController.cs
M07.WebApp.Tests/MvcControllerTests.cs
A common mistake is to be too loose here. NotFound() returns NotFoundResult, while NotFound(value) returns NotFoundObjectResult. Both mean 404, but they are different types. BeOfType<NotFoundResult>() fails if the action starts sending a body. That is usually what you want: a change in the response shape should be a decision, not an accident.
Summary
- In a unit test, the
IActionResultobject is the contract. Razor does not run, so assert the result, not the HTML. - Check the result type, the view model, the route values of a redirect and the status it maps to.
- Use
.Subjectand.Whichto continue asserting on the cast value without manual casts. - Prefer
nameof(...)over string action names in redirect assertions. - Cover the
NotFoundpath for every action that takes an id.