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|vb] class to open that file.
  4. Right-click at any point of the StoreController class code and then select Create Unit Tests.

    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#|Basic] test project is selected in the Output project select box. Finally, click OK.

    Figure 2

    Selecting the tests to create – C#

    Figure 3

    Selecting the tests to create –VB

    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 4

    Creating a New Test Project

  7. A new project is created.

    Figure 5

    Solution Explorer with the new Test Project – C#

    Figure 6

    Solution Explorer with the new Test Project - VB

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.

    Figure 7

    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("https://localhost:1045/")]

    Visual Basic

    <TestMethod()> <HostType("ASP.NET")> <AspNetDevelopmentServerHost("D:\\ Source\\Ex01-Testing simple controller actions\\Begin\\MvcMusicStore", "/")> <UrlToTest("https://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:

    C#

    [TestMethod()] public void IndexTest()

    Visual Basic

    <TestMethod()> public void IndexTest()

    C#

    [TestMethod()] public void DetailsTest()

    Visual Basic

    <TestMethod()> public void DetailsTest()

    C#

    [TestMethod()] public void BrowseTest()

    Visual Basic

    <TestMethod()> public void BrowseTest()

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

    Figure 8

    Running the tests

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

    Figure 9

    Tests Results

Task 3 – Implementing the test for the Index action method

Now that the tests fail, you will start making them pass one by one. In this task you will implement the test for the Index action method of the Store Controller. The Index method provided in the Begin solution only returns this text: "Hello from Store.Index()", hence that is the text that your test method should verify.

  1. In the StoreControllerTest.[cs|vb] file, replace the IndexTest method. Note that the Assert.Inconclusive method was removed with the real implementation for the test.

    (Code Snippet – ASP.NET MVC 3.0 Testing – Ex1 IndexTestMethod – CSharp)

    C#

    /// <summary> ///A test for Index ///</summary> [TestMethod()] public void IndexTest() { StoreController target = new StoreController(); string expected = "Hello from Store.Index()"; string actual; actual = target.Index(); Assert.AreEqual(expected, actual); }

    (Code Snippet – ASP.NET MVC 3.0 Testing – Ex1 IndexTestMethod – VB)

    Visual Basic

    ''' <summary> '''A test for Index '''</summary> <TestMethod()> Public Sub IndexTest() Dim target As New StoreController() Dim expected As String = "Hello from Store.Index()" Dim actual As String actual = target.Index() Assert.AreEqual(expected, actual) End Sub

    Note:
    Error: Note format was corrupted. Title should be bold.The Assert.AreEqual is a method that verifies that two objects are equal. This overload in particular compares two strings.

  2. Run exclusively this test to verify the results. To do this, right-click inside the IndexTest method and then select Run Tests.

    Figure 10

    Running tests in Current Context – C#

    Figure 11

    Running tests in Current Context - VB

  3. The Test Result window should show that the test passes.

    Figure 12

    Running test IndexTest

Task 4 – Implementing the test for the Browse action method

In this task you will implement the test for the Browse action method of the Store Controller. The Browse method provided in the Begin solution returns a text concatenating the genre received as parameter. So, your method should verify that text is returned.

  1. In the StoreControllerTest.[cs|vb] file, replace the BrowseTest method. Note that the Assert.Inconclusive method was removed with the real implementation for the test.

    (Code Snippet – ASP.NET MVC 3.0 Testing – Ex1 BrowseTestMethod – CSharp)

    C#

    /// <summary> ///A test for Browse ///</summary> [TestMethod()] public void BrowseTest() { StoreController target = new StoreController(); string genre = "Disco"; string expected = "Store.Browse, Genre = Disco"; string actual; actual = target.Browse(genre); Assert.AreEqual(expected, actual); }

    (Code Snippet – ASP.NET MVC 3.0 Testing – Ex1 BrowseTestMethod – VB)

    Visual Basic

    ''' <summary> '''A test for Browse '''</summary> <TestMethod()> Public Sub BrowseTest() Dim target As New StoreController() Dim genre As String = "Disco" Dim expected As String = "Store.Browse, Genre = Disco" Dim actual As String actual = target.Browse(genre) Assert.AreEqual(expected, actual) End Sub
  2. Run exclusively this test to verify the results. To do this, right-click inside the BrowseTest method and then select Run Tests.
  3. The Test Result window should show that the test passes.

    Figure 13

    Running test IndexTest

Task 5 – Implementing the test for the Details action method

In this task you will implement the test for the Details action method of the Store Controller. The Details method provided in the Begin solution returns a text concatenating the id received as a parameter. So, your method should verify that text is returned.

  1. In the StoreControllerTest.[cs|vb] file, replace the DetailsTest method. Note that the Assert.Inconclusive method was removed with the real implementation for the test.

    (Code Snippet – ASP.NET MVC 3.0 Testing – Ex1 DetailsTestMethod – CSharp)

    C#

    /// <summary> ///A test for Details ///</summary> [TestMethod()] public void DetailsTest() { StoreController target = new StoreController(); int id = 5; string expected = "Store.Details, ID = 5"; string actual; actual = target.Details(id); Assert.AreEqual(expected, actual); }

    (Code Snippet – ASP.NET MVC 3.0 Testing – Ex1 DetailsTestMethod – VB)

    Visual Basic

    ''' <summary> '''A test for Details '''</summary> <TestMethod()> Public Sub DetailsTest() Dim target As New StoreController() Dim id As Integer = 5 Dim expected As String = "Store.Details, ID = 5" Dim actual As Stri`ng actual = target.Details(id) Assert.AreEqual(expected, actual) End Sub

  2. Run exclusively this test to verify the results. To do this, right-click inside the DetailsTest method and then select Run Tests.
  3. The Test Result window should show that the test passes.

    Figure 14

    Running test IndexTest

Next Step

Exercise 2: Testing CRUD actions