DOCX

introduction to ASP.NET MVC 3 - Testing

By Mary Barnes,2014-09-20 15:14
17 views 0
introduction to ASP.NET MVC 3 - Testing

    Hands-On Lab ASP.NET MVC 3 Testing

Lab version: 1.1.0

Last updated: 9/20/2011

    Page 1

CONTENTS

    OVERVIEW ............................................................................................................................................. 3

    EXERCISE 1: TESTING SIMPLE CONTROLLER ACTIONS .................................................................. 5

    Task 1 Creating a Testing Project ................................................................................................... 5

    Task 2 Running the tests ............................................................................................................... 8

    Task 3 Implementing the test for the Index action method.......................................................... 11

    Task 4 Implementing the test for the Browse action method....................................................... 12

    Task 5 Implementing the test for the Details action method ....................................................... 13 EXERCISE 2: TESTING CRUD ACTIONS ............................................................................................ 14

    Task 1 Reviewing the provided solution ...................................................................................... 14

    Task 2 Adding transaction capabilities to the Test Methods ........................................................ 15

    Task 2 Testing Create Action Method .......................................................................................... 18

    Task 3 Testing Delete Action Method .......................................................................................... 19

    Task 4 Testing Edit Action Method .............................................................................................. 21

    Task 5 Testing Index Action Method ............................................................................................ 22

    Task 6 Testing Details Action Method .......................................................................................... 23

    Task 7 Testing Browse Action Method for StoreController........................................................... 24 EXERCISE 3: TESTING CART ACTIONS ............................................................................................. 26

    Task 1 Running ShoppingCartController Tests ............................................................................. 26

    Task 2 Implementing the test for the AddToCart action method ................................................. 27

    Task 3 Implementing the test for the Index action method.......................................................... 29

    Task 4 Implementing the test for the RemoveFromCart action method ....................................... 31 SUMMARY ............................................................................................................................................ 33

    Page 2

Overview

    Note: This Hands-on Lab assumes you have basic knowledge of ASP.NET MVC. If you have not used ASP.NET MVC before, we recommend you to go over ASP.NET MVC Fundamentals Hand-on Lab.

    Testing an application is an automated way of verifying it is always behaving the way it is supposed to. That way, you can later make changes to the code and refactor components without breaking something else somewhere in the application.

    This Lab will take you through the process of testing controllers in an ASP.NET MVC application, used to verify that actions are working properly.

    Unit testing is a particular method where each test isolates and verifies a single part of the application, in an isolated way, with the possibility of automating tests. A unit test is the smallest testable part of an application. In procedural programming may be a procedure, in object oriented programming, a method. Unit tests are created by programmers, and their objective is to isolate each part of a program and verify that individual parts are correct. Typically in unit testing, substitutes like method stubs, mock objects, fakes and test harnesses can be used to guarantee that isolation.

    In this Hands-on Lab, each test is independent from the others, but instead of stubs or mocks, you will be testing the controllers against a well-known database. If needed, that database will be protected by transactions and its initial state will be copied for each test execution, making sure that each test runs with a controlled data set.

    You will first focus on testing simple controller actions, then CRUD actions and finally the shopping cart actions implemented in the ASP.NET MVC Membership, Authorization & Store Checkout Hands-on Lab. Disclaimer: Test Driven Development (TDD) is a software development process where failing unit tests that define an improvement/function are written before the actual code that implements that behavior. The test passes after the implementation is done according to the test specifications. Finally, the code is refactored to match standards, improve efficiency, etc.

    Since you will be testing the MVC Music Store code built throughout the past Hands-on labs, you will not be strictly practicing TDD. Else, you will create tests to verify the behavior of already-written code.

Objectives

    In this Hands-On Lab, you will learn how to:

    ; Test simple controller actions

    ; Test actions that interact with a database

    Page 3

    ; Test actions that use the user’s session

    System Requirements

    You must have the following items to complete this lab:

    ; ASP.NET and ASP.NET MVC 3

    ; Visual Studio 2010 Professional or superior

    ; SQL Server Database (Express edition or above)

    Note: You can install the previous system requirements by using the Web Platform Installer 3.0: http://go.microsoft.com/fwlink/?LinkID=194638.

Setup

    For convenience, much of the code used in this hands-on lab is available as Visual Studio code snippets.

    To check the prerequisites of the lab and install the code snippets:

    1. Open a Windows Explorer window and browse to the lab’s Source\Setup folder.

    2. Double-click the Dependencies.dep file in this folder to launch the Dependency Checker tool

    and install any missing prerequisites and the Visual Studio code snippets.

    3. If the User Account Control dialog is shown, confirm the action to proceed. Note: This process may require elevation. The .dep extension is associated with the Dependency Checker tool during its installation. For additional information about the setup procedure and how to

    install the Dependency Checker tool, refer to the Setup.docx document in the Assets folder of the

    training kit.

Exercises

    This Hands-On Lab is comprised by the following exercises:

    1. Exercise 1: Testing simple controller actions

    2. Exercise 2: Testing CRUD actions

    3. Exercise 3: Testing cart actions

Estimated time to complete this lab: 35 minutes.

    Page 4

    Note: Each exercise is accompanied by an End folder containing the resulting solution you should obtain after completing the exercises. You can use this solution as a guide if you need additional help

    working through the exercises.

Next Step

    Exercise 1: Testing simple controller actions

    Exercise 1: Testing simple controller actions

    In this exercise, you will learn how to test your controllers by adding testing methods to verify the

    behavior of its method actions.

    Task 1 Creating a Testing Project

    In this task you will create a testing project for your tests.

    1. Start Microsoft Visual Studio 2010 Professional from Start | All Programs | Microsoft Visual

    Studio 2010 | Microsoft Visual Studio 2010.

    2. In the File menu, choose Open Project. In the Open Project dialog, browse to Source\Ex01-

    Testing simple controller actions\Begin, select MvcMusicStore.sln and click Open.

    3. In the Solution Explorer expand the Controllers folder and double-click the StoreController.cs

    class to open that file.

    4. Right-click at any point of the StoreController class code and then select Create Unit Tests.

    Page 5

Figure 1

    Creating Unit Tests

    5. A window appears with a tree to select the methods to test. Uncheck StoreController()

    because you will not add any test to the class constructor, and verify that

    Browse(System.String), Details(System.Int32) and Index() methods are selected. Verify that the option Create a new Visual C# test project is selected in the Output project select box. Finally, click OK.

    Page 6

Figure 2

    Selecting the tests to create

Note: This process creates a project, adds a reference to the project to be tested and creates

    skeleton methods that you will complete to test your controller actions.

    6. In the New Test Project window change the project name to MvcMusicStore.Tests and click Create.

Figure 3

    Creating a New Test Project

    Page 7

    7. A new project is created.

    Figure 4

    Solution Explorer with the new Test Project

Task 2 Running the tests

    In this task you will run the tests to verify the system’s behavior. These tests were marked as Inconclusive: an Assert.Inconclusive method is invocated, raising an error. This is a way to verify that

    your tests did not pass because they were not implemented.

    1. Build the Solution to verify everything is in order. To do this, In the Build Menu, select Build

    Solution. The Solution should build succesfully.

    Page 8

Figure 5

    Building the project

    2. The tests were created in a default ASP.NET environment. In order to test the controllers but without the need to run the web application, you must delete the following attributes before each of the 3 test methods.

    C#

    [TestMethod()]

    [HostType("ASP.NET")]

    [AspNetDevelopmentServerHost("D:\\ Source\\Ex01-Testing simple controller

    actions\\Begin\\MvcMusicStore", "/")]

    [UrlToTest("http://localhost:1045/")]

    Note: These attributes avoid you to configure these tests to run through ASP.NET. Unit tests typically run under the VSTest host process. ASP.NET unit tests must run under the ASP.NET host process. This is configured by the HostType attribute. Additionally, you should specify the

    path to the web application by the AspNetDevelopmentServerHost attribute. Finally, you have

    to indicate Visual Studio the URL to test by defining the UrlToTest attribute.

    In your scenario, you will test your controller action’s methods and you don’t need to configure this.

Tests should look like the following:

    Page 9

C#

    [TestMethod()]

    public void IndexTest()

C#

    [TestMethod()]

    public void DetailsTest()

C#

    [TestMethod()]

    public void BrowseTest()

    3. Run the tests. To do this, in Test Menu select Run, and then All Tests in Solution.

Figure 6

    Running the tests

    4. Verify that all tests fail in the Test Results window.

Figure 7

    Tests Results

    Page 10

Report this document

For any questions or suggestions please email
cust-service@docsford.com