Unit testing the controller
Unit testing is a software development process in which the smallest testable parts of an application, called units, are individually and independently tested for correct operation. Typically, these units are individual methods being tested. Most often, unit tests are run automatically, and provide immediate feedback (successful/unsuccessful/unknown result) to a developer on the changes he or she has just made to the code. If a test is unsuccessful, the changes to the code should be reviewed because the expected behavior of a portion of source code has changed and may affect other units or the application as a whole.
When we created the ASP.NET MVC web application, a test project was also created. This already contains an example test class for HomeController, testing both the Index and About actions.
In the MvcApplication1Tests project, right-click on the Controllers folder, and then select Add | Unit Test from the context menu. From the wizard that is displayed, select the Show method of EmployeeController and click on OK. Visual Studio will generate a test class.
Modify the generated test class to look like the following code:
using System.Web.Mvc;using Microsoft.VisualStudio.TestTools.UnitTesting;using MvcApplication1.Controllers;
namespace MvcApplication1Tests.Controllers{ /// <summary> /// Summary description for EmployeeControllerTest /// </summary>
[TestClass] public class EmployeeControllerTest { [TestMethod] public void show_action_creates_employee_and_passes_to_view_when_firstname_is_specified() { // Setup EmployeeController controller = new EmployeeController(); // Execute ViewResult result = controller.Show("Maarten") as ViewResult; // Verify Assert.IsNotNull(result); ViewDataDictionary viewData = result.ViewData; Assert.IsNotNull(viewData.Model); Assert.AreEqual("Maarten", (viewData.Model as MvcApplication1.Models.Employee).FirstName); Assert.IsNull(viewData["ErrorMessage"]); } [TestMethod] public void show_action_passes_error_model_to_view_when_no_firstname_specified() { // Setup EmployeeController controller = new EmployeeController(); // Execute ViewResult result = controller.Show(null) as ViewResult; // Verify Assert.IsNotNull(result); ViewDataDictionary viewData = result.ViewData; Assert.IsNull(viewData.Model); Assert.IsNotNull(viewData["ErrorMessage"]); } }}
Each test method is first initializing a new EmployeeController, after which the action method that needs to be tested is called. When the action method returns an ActionResult, it is cast to ViewResult on which some assertions are made. For example, if the Show action method of EmployeeController is called with parameter Maarten, an assertion is made that the controller passes the correct employee data to the view.
This test suite does not require the application to be deployed on a web server. Instead, the EmployeeController is tested directly in the code. The test asserts that some properties of the ViewData are present. For example, if the Show() action is called with the parameter, Maarten, the Model should not be null and should contain an Employee with the first name, Maarten.
More advanced testing scenarios are explained in Chapter 9, Testing an Application.