Introduction to FluentMvcTesting

This library provides a fluent interface for creating terse, expressive and (where possible) type-safe tests against ASP.NET MVC controllers.

There are two types of tests you might want to use it for:

  • Unit tests against the MVC Controllers (e.g. with dependencies mocked out)
  • Subcutaneous tests against the MVC Controllers (e.g. with real dependencies)

This library is testing framework agnostic, so you can combine it with the testing library of your choice (e.g. NUnit, xUnit, etc.).

The library is compatible with the AAA testing methodology, although it combines the Act and Assert parts together depending on how you use it. See the code examples for an idea of what the syntax looks like.

This library was originally inspired by the MVCContrib.TestHelper library.

Using FluentMvcTesting for unit tests

In general we recommend that you keep your MVC controllers really lean (2 or 3 lines) in which case the controller probably doesn't need a test since it will be clear at a glance that it's correct or not and it will be unlikely to have regressions. This library is great for those situations where you have to have a meaty controller, which in turn warrants test coverage.

Using FluentMvcTesting for subcutaneous tests

This library works really well when writing subcutaneous tests against a controller. More specifically, if you are writing your tests in a BDD style then the types of assertions that you can easily and quickly perform with this library are quite helpful. For instance, you may want to assert that after the user performs an action that they get shown a success page - this can easily be achieved by ensuring the ActionResult returned by the MVC action is a redirect to the Success action. For a more complete explanation of this technique including a real-world code sample please see the "Subcutaneous testing" section of Rob's blog post.

Motivation

The motivation behind this library is to provide a way to test MVC actions quickly, tersely and maintainably.

Most examples we find on MVC controller testing are incredibly verbose, repetitive and time-consuming to write and maintain and we've also noticed most examples of controller testing having a lot of magic strings being used to test view and action names.

This library aims to make the time to implement controller tests really small for those situations where you find yourself needing to write them and it also aims to (where possible) utilize the type system to resolve a lot of those nasty magic strings thus ensuring your tests are more maintainable and require less re-work when you perform major refactoring of your code.