SPS Home > Dgreath > C# MVC Unit Testing |
C# MVC Unit TestingThe standard practice in MVC development is step by step incremental unit testing to assure that each method performs as expected in an isolated environment. This serves both the immediate goal of assuring a quality software product and to provide a baseline for future requalification as the result of down the road design changes. This document provides a step by step procedure for implementing unit testing in Visual Studio C# projects. |
Step One: Create Test Project
Assuming you are in the project to be tested, start by creating a new test project in Visual Studio: |
Step Two: Reference the relevant MVC namespace
Right click on References in Solution Explorer then select Add New Reference.
Navigate to the appropriate version of |
Step Three: Reference the project to be testedRight click on References in Solution Explorer, then select Add New Reference. Navigate to the project to be tested and add it. |
Step Four: Create testsFor each test to be created, right click Add, then Class, select Unit Test to create a new class. Each test will require its own class. |
Step Five: Add reference to project's controller namespace
Add the following statement at the top of the page for each newly created class: |
Step Six: Write the test
The standard class produced includes a constructor and skeletons for various
properties. In most cases these can be deleted as the single method is the
concern. The unit test is called an assertion and is built like a standard
method except that the final line is an
Examples of typical tests follow below. |
Step Seven: Run the testTest > Run > All Tests In Solution |
Step Eight: Review results, make corrections, refactorThe initial test should be designed to fail the first time. This assures that the test is capable of detecting errors when they occur.Information reported from subsequent test execution should provide useful information as to why the test failed. Make the nessary corrections and re-run the test. Once all issues are resolved, then you can refactor and move on to the next test, retaiing this test for QA review and as a future baseline test. |
Basic Test Examples |
Test View Returned By Controller
The following method calls the controller "HomeController" and verifies that the
returned view for the default case is "Index".
Option: Messages can be included in the assertions to report specific reasons for
failure like this: |
Test Data Returned By Controller
Here, a hypothetical ProductDetails method of the ProductController is called
with a value of 5 passed in as the product id: |
Test Action Result Returned By Controller
Occasionally when processing an HttpPost, validation may determine that the process must be divert to another
handler. The following method tests that behavior: |
Test Json Result Returned By Controller
In this example a call into a Json controller will return a Json string to the response stream.
Select one or more fields of interest in the Json payload and set up asserts for them. Any number
of fields/asserts can be tested at one time. |
Test Business Layer Objects
Here, a hypothetical business layer object "ProductDetails" method is called with a value of 5 passed in
as the product id: |