"
ASP.NET (snapshot 2017) Microsoft documentation and samples

Iteration #5 – Create unit tests (C#)

by Microsoft

Download Code

In the fifth iteration, we make our application easier to maintain and modify by adding unit tests. We mock our data model classes and build unit tests for our controllers and validation logic.

Building a Contact Management ASP.NET MVC Application (C#)

In this series of tutorials, we build an entire Contact Management application from start to finish. The Contact Manager application enables you to store contact information - names, phone numbers and email addresses - for a list of people.

We build the application over multiple iterations. With each iteration, we gradually improve the application. The goal of this multiple iteration approach is to enable you to understand the reason for each change.

This Iteration

In the previous iteration of the Contact Manager application, we refactored the application to be more loosely coupled. We separated the application into distinct controller, service, and repository layers. Each layer interacts with the layer beneath it through interfaces.

We refactored the application to make the application easier to maintain and modify. For example, if we need to use a new data access technology, we can simply change the repository layer without touching the controller or service layer. By making the Contact Manager loosely coupled, we ve made the application more resilient to change.

But, what happens when we need to add a new feature to the Contact Manager application? Or, what happens when we fix a bug? A sad, but well proven, truth of writing code is that whenever you touch code you create the risk of introducing new bugs.

For example, one fine day, your manager might ask you to add a new feature to the Contact Manager. She wants you to add support for Contact Groups. She wants you to enable users to organize their contacts into groups such as Friends, Business, and so on.

In order to implement this new feature, you’ll need to modify all three layers of the Contact Manager application. You’ll need to add new functionality to the controllers, the service layer, and the repository. As soon as you start modifying code, you risk breaking functionality that worked before.

Refactoring our application into separate layers, as we did in the previous iteration, was a good thing. It was a good thing because it enables us to make changes to entire layers without touching the rest of the application. However, if you want to make the code within a layer easier to maintain and modify, you need to create unit tests for the code.

You use a unit test to test an individual unit of code. These units of code are smaller than entire application layers. Typically, you use a unit test to verify whether a particular method in your code behaves in the way that you expect. For example, you would create a unit test for the CreateContact() method exposed by the ContactManagerService class.

The unit tests for an application work just like a safety net. Whenever you modify code in an application, you can run a set of unit tests to check whether the modification breaks existing functionality. Unit tests make your code safe to modify. Unit tests make all of the code in your application more resilient to change.

In this iteration, we add unit tests to our Contact Manager application. That way, in the next iteration, we can add Contact Groups to our application without worrying about breaking existing functionality.

[!NOTE]

There are a variety of unit testing frameworks including NUnit, xUnit.net, and MbUnit. In this tutorial, we use the unit testing framework included with Visual Studio. However, you could just as easily use one of these alternative frameworks.

What Gets Tested

In the perfect world, all of your code would be covered by unit tests. In the perfect world, you would have the perfect safety net. You would be able to modify any line of code in your application and know instantly, by executing your unit tests, whether the change broke existing functionality.

However, we don t live in a perfect world. In practice, when writing unit tests, you concentrate on writing tests for your business logic (for example, validation logic). In particular, you do not write unit tests for your data access logic or your view logic.

To be useful, unit tests must execute very quickly. You easily can accumulate hundreds (or even thousands) of unit tests for an application. If the unit tests take a long time to run then you’ll avoid executing them. In other words, long running unit tests are useless for day to day coding purposes.

For this reason, you typically do not write unit tests for code that interacts with a database. Running hundreds of unit tests against a live database would be too slow. Instead, you mock your database and write code that interacts with the mock database (we discuss mocking a database below).

For a similar reason, you typically do not write unit tests for views. In order to test a view, you must spin up a web server. Because spinning up a web server is a relatively slow process, creating unit tests for your views is not recommended.

If your view contains complicated logic then you should consider moving the logic into Helper methods. You can write unit tests for Helper methods that execute without spinning up a web server.

[!NOTE]

While writing tests for data access logic or view logic is not a good idea when writing unit tests, these tests can be very valuable when building functional or integration tests.

[!NOTE]

ASP.NET MVC is the Web Forms View Engine. While the Web Forms View Engine is dependent on a web server, other view engines might not be.

Using a Mock Object Framework

When building unit tests, you almost always need to take advantage of a Mock Object framework. A Mock Object framework enables you to create mocks and stubs for the classes in your application.

For example, you can use a Mock Object framework to generate a mock version of your repository class. That way, you can use the mock repository class instead of the real repository class in your unit tests. Using the mock repository enables you to avoid executing database code when executing a unit test.

Visual Studio does not include a Mock Object framework. However, there are several commercial and open source Mock Object frameworks available for the .NET framework:

  1. Moq - This framework is available under the open source BSD license. You can download Moq from https://code.google.com/p/moq/.
  2. Rhino Mocks - This framework is available under the open source BSD license. You can download Rhino Mocks from http://ayende.com/projects/rhino-mocks.aspx.
  3. Typemock Isolator - This is a commercial framework. You can download a trial version from http://www.typemock.com/.

In this tutorial, I decided to use Moq. However, you could just as easily use Rhino Mocks or Typemock Isolator to create the Mock objects for the Contact Manager application.

Before you can use Moq, you need to complete the following steps:

  1. .
  2. Before you unzip the download, make sure that you right-click the file and click the button labeled Unblock (see Figure 1).
  3. Unzip the download.
  4. Add a reference to the Moq assembly by right-clicking the References folder in the ContactManager.Tests project and selecting Add Reference. Under the Browse tab, browse to the folder where you unzipped Moq and select the Moq.dll assembly. Click the OK button.
  5. After you complete these steps, your References folder should look like Figure 2.

Unblocking Moq

Figure 01: Unblocking Moq(Click to view full-size image)

References after adding Moq

Figure 02: References after adding Moq(Click to view full-size image)

Creating Unit Tests for the Service Layer

Let s start by creating a set of unit tests for our Contact Manager application s service layer. We’ll use these tests to verify our validation logic.

Create a new folder named Models in the ContactManager.Tests project. Next, right-click the Models folder and select Add, New Test. The Add New Test dialog shown in Figure 3 appears. Select the Unit Test template and name your new test ContactManagerServiceTest.cs. Click the OK button to add your new test to your Test Project.

[!NOTE]

In general, you want the folder structure of your Test Project to match the folder structure of your ASP.NET MVC project. For example, you place controller tests in a Controllers folder, model tests in a Models folder, and so on.

Models.cs

Figure 03: Models.cs(Click to view full-size image)

Initially, we want to test the CreateContact() method exposed by the ContactManagerService class. We’ll create the following five tests:

The first test verifies that a valid Contact does not generate a validation error. The remaining tests check each of the validation rules.

The code for these tests is contained in Listing 1.

Listing 1 - Models.cs

[!code-csharpMain]

   1:  using System.Web.Mvc;
   2:  using ContactManager.Models;
   3:  using Microsoft.VisualStudio.TestTools.UnitTesting;
   4:  using Moq;
   5:   
   6:  namespace ContactManager.Tests.Models
   7:  {
   8:      [TestClass]
   9:      public class ContactManagerServiceTest
  10:      {
  11:          private Mock<IContactManagerRepository> _mockRepository;
  12:          private ModelStateDictionary _modelState;
  13:          private IContactManagerService _service;
  14:   
  15:          [TestInitialize]
  16:          public void Initialize()
  17:          {
  18:              _mockRepository = new Mock<IContactManagerRepository>();
  19:              _modelState = new ModelStateDictionary();
  20:              _service = new ContactManagerService(new ModelStateWrapper(_modelState), _mockRepository.Object);
  21:          }
  22:   
  23:          [TestMethod]
  24:          public void CreateContact()
  25:          {
  26:              // Arrange
  27:              var contact = Contact.CreateContact(-1, "Stephen", "Walther", "555-5555", "steve@somewhere.com");
  28:   
  29:              // Act
  30:              var result = _service.CreateContact(contact);
  31:          
  32:              // Assert
  33:              Assert.IsTrue(result);
  34:          }
  35:   
  36:          [TestMethod]
  37:          public void CreateContactRequiredFirstName()
  38:          {
  39:              // Arrange
  40:              var contact = Contact.CreateContact(-1, string.Empty, "Walther", "555-5555", "steve@somewhere.com");
  41:   
  42:              // Act
  43:              var result = _service.CreateContact(contact);
  44:   
  45:              // Assert
  46:              Assert.IsFalse(result);
  47:              var error = _modelState["FirstName"].Errors[0];
  48:              Assert.AreEqual("First name is required.", error.ErrorMessage);
  49:          }
  50:   
  51:          [TestMethod]
  52:          public void CreateContactRequiredLastName()
  53:          {
  54:              // Arrange
  55:              var contact = Contact.CreateContact(-1, "Stephen", string.Empty, "555-5555", "steve@somewhere.com");
  56:   
  57:              // Act
  58:              var result = _service.CreateContact(contact);
  59:   
  60:              // Assert
  61:              Assert.IsFalse(result);
  62:              var error = _modelState["LastName"].Errors[0];
  63:              Assert.AreEqual("Last name is required.", error.ErrorMessage);
  64:          }
  65:   
  66:          [TestMethod]
  67:          public void CreateContactInvalidPhone()
  68:          {
  69:              // Arrange
  70:              var contact = Contact.CreateContact(-1, "Stephen", "Walther", "apple", "steve@somewhere.com");
  71:   
  72:              // Act
  73:              var result = _service.CreateContact(contact);
  74:   
  75:              // Assert
  76:              Assert.IsFalse(result);
  77:              var error = _modelState["Phone"].Errors[0];
  78:              Assert.AreEqual("Invalid phone number.", error.ErrorMessage);
  79:          }
  80:   
  81:          [TestMethod]
  82:          public void CreateContactInvalidEmail()
  83:          {
  84:              // Arrange
  85:              var contact = Contact.CreateContact(-1, "Stephen", "Walther", "555-5555", "apple");
  86:   
  87:              // Act
  88:              var result = _service.CreateContact(contact);
  89:   
  90:              // Assert
  91:              Assert.IsFalse(result);
  92:              var error = _modelState["Email"].Errors[0];
  93:              Assert.AreEqual("Invalid email address.", error.ErrorMessage);
  94:          }
  95:      }
  96:  }

Because we use the Contact class in Listing 1, we need to add a reference to the Microsoft Entity Framework to our Test project. Add a reference to the System.Data.Entity assembly.

Listing 1 contains a method named Initialize() that is decorated with the [TestInitialize] attribute. This method is called automatically before each of the unit tests is run (it is called 5 times right before each of the unit tests). The Initialize() method creates a mock repository with the following line of code:

[!code-csharpMain]

   1:  _mockRepository = new Mock<IContactManagerRepository>();

This line of code uses the Moq framework to generate a mock repository from the IContactManagerRepository interface. The mock repository is used instead of the actual EntityContactManagerRepository to avoid accessing the database when each unit test is run. The mock repository implements the methods of the IContactManagerRepository interface, but the methods don t actually do anything.

[!NOTE]

When using the Moq framework, there is a distinction between _mockRepository and _mockRepository.Object. The former refers to the Mock<IContactManagerRepository> class that contains methods for specifying how the mock repository will behave. The latter refers to the actual mock repository that implements the IContactManagerRepository interface.

The mock repository is used in the Initialize() method when creating an instance of the ContactManagerService class. All of the individual unit tests use this instance of the ContactManagerService class.

Listing 1 contains five methods that correspond to each of the unit tests. Each of these methods is decorated with the [TestMethod] attribute. When you run the unit tests, any method that has this attribute is called. In other words, any method that is decorated with the [TestMethod] attribute is a unit test.

The first unit test, named CreateContact(), verifies that calling CreateContact() returns the value true when a valid instance of the Contact class is passed to the method. The test creates an instance of the Contact class, calls the CreateContact() method, and verifies that CreateContact() returns the value true.

The remaining tests verify that when the CreateContact() method is called with an invalid Contact then the method returns false and the expected validation error message is added to model state. For example, the CreateContactRequiredFirstName() test creates an instance of the Contact class with an empty string for its FirstName property. Next, the CreateContact() method is called with the invalid Contact. Finally, the test verifies that CreateContact() returns false and that model state contains the expected validation error message “First name is required.”

You can run the unit tests in Listing 1 by selecting the menu option Test, Run, All Tests in Solution (CTRL+R, A). The results of the tests are displayed in the Test Results window (see Figure 4).

Test Results

Figure 04: Test Results (Click to view full-size image)

Creating Unit Tests for Controllers

ASP.NETMVC application control the flow of user interaction. When testing a controller, you want to test whether the controller returns the right action result and view data. You also might want to test whether a controller interacts with model classes in the manner expected.

For example, Listing 2 contains two unit tests for the Contact controller Create() method. The first unit test verifies that when a valid Contact is passed to the Create() method then the Create() method redirects to the Index action. In other words, when passed a valid Contact, the Create() method should return a RedirectToRouteResult that represents the Index action.

We don t want to test the ContactManager service layer when we are testing the controller layer. Therefore, we mock the service layer with the following code in the Initialize method:

[!code-csharpMain]

   1:  _service = new Mock();

In the CreateValidContact() unit test, we mock the behavior of calling the service layer CreateContact() method with the following line of code:

[!code-csharpMain]

   1:  _service.Expect(s => s.CreateContact(contact)).Returns(true);

This line of code causes the mock ContactManager service to return the value true when its CreateContact() method is called. By mocking the service layer, we can test the behavior of our controller without needing to execute any code in the service layer.

The second unit test verifies that the Create() action returns the Create view when an invalid contact is passed to the method. We cause the service layer CreateContact() method to return the value false with the following line of code:

[!code-csharpMain]

   1:  _service.Expect(s => s.CreateContact(contact)).Returns(false);

If the Create() method behaves as we expect then it should return the Create view when the service layer returns the value false. That way, the controller can display the validation error messages in the Create view and the user has a chance to correct that invalid Contact properties.

If you plan to build unit tests for your controllers then you need to return explicit view names from your controller actions. For example, do not return a view like this:

return View();

Instead, return the view like this:

return View(“Create”);

If you are not explicit when returning a view then the ViewResult.ViewName property returns an empty string.

Listing 2 - Controllers.cs

[!code-csharpMain]

   1:  using System.Web.Mvc;
   2:  using ContactManager.Controllers;
   3:  using ContactManager.Models;
   4:  using Microsoft.VisualStudio.TestTools.UnitTesting;
   5:  using Moq;
   6:   
   7:  namespace ContactManager.Tests.Controllers
   8:  {
   9:      [TestClass]
  10:      public class ContactControllerTest
  11:      {
  12:          private Mock<IContactManagerService> _service;
  13:   
  14:          [TestInitialize]
  15:          public void Initialize()
  16:          {
  17:              _service = new Mock<IContactManagerService>();
  18:          }
  19:   
  20:          [TestMethod]
  21:          public void CreateValidContact()
  22:          {
  23:              // Arrange
  24:              var contact = new Contact();
  25:              _service.Expect(s => s.CreateContact(contact)).Returns(true);
  26:              var controller = new ContactController(_service.Object);
  27:          
  28:              // Act
  29:              var result = (RedirectToRouteResult)controller.Create(contact);
  30:   
  31:              // Assert
  32:              Assert.AreEqual("Index", result.RouteValues["action"]);
  33:          }
  34:   
  35:          [TestMethod]
  36:          public void CreateInvalidContact()
  37:          {
  38:              // Arrange
  39:              var contact = new Contact();
  40:              _service.Expect(s => s.CreateContact(contact)).Returns(false);
  41:              var controller = new ContactController(_service.Object);
  42:   
  43:              // Act
  44:              var result = (ViewResult)controller.Create(contact);
  45:   
  46:              // Assert
  47:              Assert.AreEqual("Create", result.ViewName);
  48:          }
  49:   
  50:      }
  51:  }

Summary

In this iteration, we created unit tests for our Contact Manager application. We can run these unit tests at any time to verify that our application still behaves in the manner that we expect. The unit tests act as a safety net for our application enabling us to safely modify our application in the future.

We created two sets of unit tests. First, we tested our validation logic by creating unit tests for our service layer. Next, we tested our flow control logic by creating unit tests for our controller layer. When testing our service layer, we isolated our tests for our service layer from our repository layer by mocking our repository layer. When testing the controller layer, we isolated our tests for our controller layer by mocking the service layer.

In the next iteration, we modify the Contact Manager application so that it supports Contact Groups. We’ll add this new functionality to our application using a software design process called test-driven development.

Previous Next



Comments ( )
Link to this page: //www.vb-net.com/AspNet-DocAndSamples-2017/aspnet/mvc/overview/older-versions-1/contact-manager/iteration-5-create-unit-tests-cs.htm
< THANKS ME>