9. Create unit tests for ASP. net mvc applications

Source: Internet
Author: User

This articleArticleDemonstrate how to apply ASP. NET MVCProgramCreate a unit test. Here we discuss how to create three types of Unit Tests
The test controller returns a specific view.
Test controller returns specific view Data
The test controller cap returns a specific action result.

1. Create the Controller to be tested
We name the Controller to be tested as productcontroller, Code As follows:
Listing 1-productcontroller. CS
Using system;
Using system. Web. MVC;
Namespace store. Controllers
{
Public class productcontroller: Controller
{
Public actionresult index ()
{
// Add action logic here
Throw new notimplementedexception ();
}
Public actionresult details (int id)
{
Return view ("details ");
}
}
}
The productcontroller controller contains two control actions: Index () and details (). Both controllers return to the view, and the details () Action receives an ID parameter.

2. view returned by the test controller.(Original: Home of the gray WormHttp://hi.baidu.com/grayworm)
Suppose we want to test whether productcontroller returns the correct view, ensure that productcontroller. Details () can be called, and return the Details View. The following code contains a unit test to test the productcontroller. Details () action. .
Listing 2-productcontrollertest. CS
Using system. Web. MVC;
Using Microsoft. visualstudio. testtools. unittesting;
Using store. controllers;
Namespace storetests. Controllers
{
[Testclass]
Public class Productcontrollertest
{
[Testmethod]
Public void testdetailsview ()
{
VaR controller = new productcontroller ();
VaR result = controller. Details (2) as viewresult;
Assert. areequal ("details", result. viewname );

}
}
}
In the above Code, we define a testdetailsview () method, which contains three lines of code, The first line of code is to create an instance of the productcontroller class. The second line of code calls the Controller's details () Action method. The last line of code checks whether the details () action returns to the Details View.
The viewresult. viewname attribute indicates the name of the view returned by the control layer. The Controller has two methods to return the View:
1. The controller Action explicitly returns the view name
Public actionresult details (int id) {return view ("details ");}

2. The default name of the controller action returned view is the Controller action name.
Public actionresult details (int id) {return view ();}

However, if we want to test the name of the returned view, we must explicitly return the view name in the Controller action.

We can pressCTRL + R, A to run unit test, OrClick Run all tests in solution., As shown in, if the unit test passes, we will see the test result in the test results window, as shown in

Figure 1

Figure 2

3. Test viewdata returned by the Controller(Original: Home of the gray WormHttp://hi.baidu.com/grayworm )
The MVC controller returns data to the view layer through viewdata. For example, we want to display the details of a specific product after calling the productcontroller. Details () action. Then we can create a product instance in the details () action and pass it to the details view using viewdata.
The details () Action Code is as follows, and a product information is returned.
Listing 3- Productcontroller. CS
Using system;
Using system. Web. MVC;
Namespace store. Controllers
{
Public class Productcontroller: Controller
{
Public actionresult index ()
{
// Add action logic here
Throw new notimplementedexception ();
}
Public Actionresult details (int id)
{
VaR Product = new product (ID, "laptop ");
Return view ("details", product );
}
}
}

First, in the details action, we create a product instance, which is a laptop computer, and then we pass it into the view () method.
We can write unit tests to check whether the viewdata returned by details () contains the expected data. The following code is used to test whether the productcontroller. Details () action returns a product instance of a laptop computer.
Listing 4-productcontrollertest. CS
Using system. Web. MVC;
Using Microsoft. visualstudio. testtools. unittesting;
Using store. controllers;
Namespace storetests. Controllers
{
[Testclass]
Public Class productcontrollertest
{
[Testmethod]
Public void Testdetailsviewdata ()
{
VaR controller = new productcontroller ();
VaR result = controller. Details (2) as viewresult;
VaR Product = (product) result. viewdata. model;
Assert. areequal ("laptop", product. Name );
}
}
}
In the above Code, the testdetailsview () method is used to test the viewdata returned by the details () control action. This viewdata is a details () action that appears in the form of viewresult attributes. Viewdatamodel contains the product that is passed to the view. This test is to check whether the product name in viewdata returned by the Controller action is laptop.

Iv. Test the action result returned by the Controller.(Original: Home of the gray WormHttp://hi.baidu.com/grayworm)
Complex controller actions often return different types of action results based on different input parameters. The Controller action can return various action results. Such as viewresult, redirecttorouteresult, and jsonresult.
For example, the following code, The details action receives an ID parameter. When a valid ID (ID> = 1) is passed in, the details () action returns the Details View, if the passed ID is invalid (ID <1), the details () Action jumps to the index () action.
Listing 5- Productcontroller. CS
Using system;
Using system. Web. MVC;
Namespace store. Controllers
{
Public class Productcontroller: Controller
{
Public actionresult index ()
{
// Add action logic here
Throw new notimplementedexception ();
}
Public actionresult details (int id)
{
If (ID <1)
Return redirecttoaction ("Index ");
VaR Product = new product (ID, "laptop ");
Return view ("details", product );

}
}
}

We can write the following test code to test the details () action. The following test code mainly checks whether the system can jump to the index view when we pass the-1 to details () action.
Listing 6- Productcontrollertest. CS
Using system. Web. MVC;
Using Microsoft. visualstudio. testtools. unittesting;
Using store. controllers;
Namespace storetests. Controllers
{
[Testclass]
Public class Productcontrollertest
{
[Testmethod]
Public void Testdetailsredirect ()
{
VaR controller = new productcontroller ();
VaR result = (redirecttorouteresult) controller. Details (-1 );
Assert. areequal ("Index", result. Values ["action"]);

}
}
}

When we call the redirecttoaction () method in the Controller action, the Controller action returns redirecttorouteresult. The test code checks whether redirecttorouteresult wants to redirect the user to the index action.

Summary
In this article, we will learn how to create unit tests for MVC controller actions.
First, we learn to check whether the Controller action returns the correct view. Use viewresult. viewname to verify the returned view name
Then, we will learn how to verify the returned viewdata content. Here we check whether the Controller action returns the correct product information in viewdata.
Finally, we discussed how to test whether the Controller returns multiple actionresults. Here we tested whether viewresult or redirecttorouteresult is returned by the Controller.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.