Libraries for Unit Testing MVC5 Controllers

Libraries for Unit Testing MVC5 Controllers

There are various libraries available to help you unit test your controller actions, returned views and models, and action results with MVC. One that I have recently started using is TestStack.FluentMVCTesting. This library allows you to perform actions on your controllers within your unit tests, and then verify, with a fluent syntax, that those actions behaved correctly.

Here are a couple examples: (Most code taken from their GitHub documentation):

public void Index_ShouldReturnDefaultView()
    _controller.WithCallTo(c => c.Index()).ShouldRenderDefaultView();

public void Index_ShouldReturnDefaultView_WithExpectedModel(){

var vm = new SomeViewModel();
_controller.WithModelErrors().WithCallTo(c => c.Index(vm))


public void Index_ReturnsNamedView(){

_controller.WithCallTo(c => c.Index())


These are just a few simple examples, but this library allows you to test (to name a few things):

  • Action returning a default view
  • Action returning a view with expected model
  • Action returning a named view
  • HTTP Status returned by action
  • JSON Data returned by action

I have found it super useful for unit testing my controllers. I also use it in conjunction with Compare .NET Objects, another useful library which allows you to perform equality checks on your objects using recursion. It’s a fairly robust equality checking library and I have found it especially useful when comparing actual rendered models vs expected models.

One example of why you might do this: Say you’re “newing” up a model in your controller action, setting some properties on it, and then returning it along with your view. In this case your expected unit test model and actual model will not be referentially equal, and so a call to .Equals() will always be false, unless you implement custom overrides of .Equals() on all of your models. This is not only a pain, but might be mixing concerns as well.

In conclusion, these two libraries, TestStack.FluentMVCTesting and Compare .NET Objects, allow you to easily write maintainable, thorough unit tests around your controllers, actions and models in MVC.

Let me know if you use them, and what you think.

Share Your Thoughts!


What My Clients Say

Karin - Hope and Future

Brent has always been very prompt and generally exceeds our expectations with his work. His maintenance fee is very reasonable. I strongly recommend him!  We get many compliments on our beautiful web site!


I contacted Krueger Web Design and Brent did everything.  He setup a test site with the changes so we could verify everything was correct before we transferred from my old hosting provider.  During the setup and conversion Brent was extremely professional and responsive. He responded to any issues the same day.

Dan Kaufman
Emily - GFCLT

We've had a great experience working with Krueger Web Design. Prompt responses, great ideas, effective work... all at a reasonable price! This was the perfect fit for our organization.

Mahmud - ACMC

I would love to recommend you to anyone! You have been very helpful when we needed you the most.

Brian - SEMNIC

Working with Krueger Web Design was effortless. Communicating what I wanted for the website and the results that I saw were identical. The customer service was always prompt and I am completely satisfied with the product.

Zach - Stadium Club

Exactly what I wanted! Fast turnaround time and very affordable. Answered all of our questions quickly and made adjustments promptly. Very easy to work with and puts everything into terminology that even I can understand!

Contact Krueger Web Design

Have a question for me? Need an estimate on some work? You can email me at or drop me a line using the button below:

Copyright © 2018 Krueger Web Design - All rights reserved
Web Design in Madison, WI