Chapter 26. Testing practices

 

This chapter covers

  • Designing and testing routes
  • Unit-testing controllers
  • Unit-testing custom model binders
  • Unit-testing action filters

Testing is a key tenet of any type of engineering, and software engineering is no different. Because software needs to be fully retested on every new build, the act of executing test cases can be slow and error-prone if done by hand. Creating automated tests is an accepted best practice, and ASP.NET MVC eases this effort.

Chapter 20 covered full-system testing and the specific techniques necessary to test an ASP.NET MVC application. This chapter moves beyond the most important type of testing, full-system testing, to more targeted tests that point directly to problem areas when unexpected churn occurs in the code base.

Because controllers are normal classes and actions are merely methods, we can load and execute actions and then examine the results. But even though testing controllers is simple, we must consider an important caveat. When we test a controller action, we’re only able to write assertions for the behavior we can observe. The true test of a working application is running it in a browser, and there are significant differences between viewing a page in a browser and asserting results in a controller action test.

26.1. Testing routes

26.2. Avoiding test complexity

26.3. Testing controllers

26.4. Testing model binders

26.5. Testing action filters

26.6. Summary