Unit Testing Using Visual Studio 2010
page 2 of 5
by Vince Varallo
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 27841/ 34

Step 1: Create the TripCalculator Solution

The application being built will allow a user to enter speed and time and will calculate the distance traveled.  The calculation will be built into a class which represents the business layer of an application. 

1.    Launch Visual Studio 2010.  From the start page select "New Project…"

2.    Select ASP.NET Empty Web Application from the list of templates and name the application "TripCalculator".

3.    Click on FileàAdd New Project from the main menu.

4.    The Add New Dialog will appear.  Click on "Visual C#" from the list of Installed Templates.  Choose "Class Library" from the list of templates.  Name the project "TripCalculatorBLL" and click the OK button.

5.    Visual Studio will add this project to your solution and create a class file called "Class1.cs".  Right click on the Class1.cs file in the Solution Explorer and select Rename from the pop-up menu.  Change the name to "DistanceCalculator.cs".  Visual Studio will ask if you want to rename all references to this new name, click the "Yes" button.

6.    Add the following method to the DistanceCalculator class.

public double CalculateDistance(double time, double speed)
{
    if (time < 0 || speed < 0)
    {
        throw new ArgumentOutOfRangeException();
    }
    else
    {
        return time * speed;
    }
}

7.    This method will calculate the distance given the time and speed.  If the time or speed parameters are less than zero then it throws an ArgumentOutOfRangeException.

8.    Now let's add a web form that can use this class.  Right click on the TripCalcualtor project and select AddàNew Item from the pop-up menu.

9.    Select the WebForm template and name the file Default.aspx.  Click the Add button.

10.  Add a reference to the TripCalculator project to the TripCalculatorBLL project.  To do this right click on the References in the TripCalculator project. 

11. Click Add Reference… from the pop-up menu.

12. Click on the Projects tab from the References dialog.

13. Select the TripCalculatorBLL project from the list and click the OK button.

14. Add the following HTML to the web form.

<asp:Label ID="Label1" runat="server" Text="Time:"></asp:Label>
<asp:TextBox ID="txtTime" runat="server"></asp:TextBox>
<br />
<asp:Label ID="Label2" runat="server" Text="Speed:"></asp:Label>
<asp:TextBox ID="txtSpeed" runat="server"></asp:TextBox>
<br />
<asp:Label ID="Label3" runat="server" Text="Distance:"></asp:Label>
<asp:Label ID="lblDistance" runat="server"></asp:Label>
<br />
<asp:Button ID="btnCalculate" runat="server" Text="Calculate" />

15. If you switch to Design mode your form should look like the following image.

16. Double click on the Calculate button to create the event handler.

17. Add the following using statement to the code behind page.

using TripCalculatorBLL;

18. Add the following code to the click event handler.

try
{
    DistanceCalculator distanceCalculator = new DistanceCalculator();
    lblDistance.Text = distanceCalculator.CalculateDistance
        (Convert.ToDouble(txtTime.Text), 
        Convert.ToDouble(txtSpeed.Text)).ToString();
}
catch (Exception ex)
{
    lblDistance.Text = ex.Message;
}

19. Try running the project and enter 5 for the Time and 5 for the Speed.  If you click the Calculate button you should get 25 for the Distance.


View Entire Article

User Comments

Title: 404 on link to code for this sample   
Name: Chase
Date: 2012-11-29 1:40:46 PM
Comment:
I am getting a 404 when I click the link for the code for this sample. I'd love to try this!
Title: unit testing   
Name: Avinash Chowdary
Date: 2012-06-01 8:03:30 AM
Comment:
good article and what about the asp.net web applications they don't have .Dll to refer in test project can you explain this scenario
Title: nice one   
Name: Abhishek goletar
Date: 2012-04-16 6:18:05 AM
Comment:
nice article
thks
Title: Positive Feedback   
Name: Hitesh
Date: 2012-04-16 6:15:45 AM
Comment:
very best example to undesrstand.
I like it very much..
Thank you !!!!
Title: nice article   
Name: gourik
Date: 2011-05-03 5:02:26 AM
Comment:
thanks..
Title: unit testing   
Name: nitin garg
Date: 2011-04-06 9:00:54 PM
Comment:
good article. expecting to dive deeper in next article by creating more complex test cases.
Title: Unit Testing   
Name: wasim
Date: 2011-04-06 2:00:12 PM
Comment:
awesome.
Title: Unit Testing Answer   
Name: Vince Varallo
Date: 2011-04-06 9:16:41 AM
Comment:
The purpose of unit testing is to validate that the code you wrote performs as expected. In this example the CalculateDistance method has a few different expected results depending on the parameters that were passed in. As a developer you should test all of the expected results. If at a later date you make a change to the method you still need to validate that all of the expected results are being produced. This tool allows you to automate the process of testing your code so it should be easier to maintain and ensure the application is working as expected.
Title: Unit Testing   
Name: Dhairya Shukla
Date: 2011-04-06 2:10:55 AM
Comment:
It's good understand for me, but If you can describe what is the purpose of unit testing in asp.net and how can use it.
One more think is that explain in basic and simple language






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-20 6:46:05 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search