Вы находитесь на странице: 1из 28

Module 9

Ensuring Quality by Debugging, Unit Testing and Refactoring

Module Overview
Debugging and Refactoring Code Unit Testing Code

Processing Unhandled Exceptions


Test-Driven Development

Lesson 1: Debugging and Refactoring Code


Overview of Debugging Demonstration: Enabling Debugging of a Web Application

What is Refactoring?
Extract Method Refactoring Demonstration: Extract Method Refactoring

Encapsulate Field Refactoring


Demonstration: Encapsulate Field Refactoring Extract Interface Refactoring

Demonstration: Extract Interface Refactoring

Overview of Debugging
Debugging is the process of observing the run-time behavior of

your application, and then locating and fixing logic errors.

Using a modern debugger, you can:

Break, or suspend, the execution of your application, and subsequently examine the code Evaluate and edit variables in your application

View registers
See the instructions created from your source code View the memory space used by your application

Debugging must be enabled in Project Designer and Web.config

Overview of Debugging Using Visual Studio 2010 Debugging Tools


Visual Studio 2010 Debugger includes:

Breakpoint enhancements

Newly redesigned Threads window


Parallel Stacks and Parallel Tasks debugger Enhanced DataTips Dump summary Page Improved symbol loading Debug mixed-mode and managed code on 64-bit applications

Demonstration: Enabling Debugging of a Web Application


Enable ASP.NET Debugging in Project Designer Enable Debugging in Web.config file

<configuration> <system.web> <compilation debug="true"

What is Refactoring?
Refactoring is the process of changing code to improve its

structure and readability, without affecting syntax, using Refactoring commands.

Visual C# Refactor commands


Extract Method Rename

Encapsulate Field
Extract Interface Remove Parameters Reorder Parameters

Extract Method Refactoring


Creates a new method from a code fragment in an existing

member

Benefits

Encourages best coding practices by emphasizing discrete, reusable methods. Encourages self-documenting code through good organization.

When descriptive names are used, high-level methods can read more like a series of comments.
Encourages the creation of finer-grained methods to simplify overriding. Reduces code duplication.

Demonstration: Extract Method Refactoring


Select the code to extract in the Text editor Use Extract Method content menu command

Encapsulate Field Refactoring


Creates a property from an existing field. Updates code with references to the new property.

When a field is public, other objects have direct access to

that field and can modify it, undetected by the object that owns that field.

By using properties to encapsulate that field, you can

disallow direct access to fields.

Demonstration: Encapsulate Field Refactoring


Select the member variable to encapsulate in the Text editor Use Encapsulate Field content menu command

Extract Interface Refactoring


Creates interfaces with members that originate from the

original class

Use the same subset of members from


Class Struct Interface

Extract Interface generates an interface in a new file and

positions the cursor at the beginning of the new file

Using the Extract Interface dialog box, you can specify the

members to extract into the new interface, the name of the new interface, and the name of the generated file

Demonstration: Extract Interface Refactoring


Select the class to extract an interface from in the Text editor Use Extract Interface content menu command

Lesson 2: Unit Testing Code


What is Unit Testing? Steps to Creating a Unit Test

Demonstration: Creating and Running a Unit Test


Optional Unit Test Steps

What is Unit Testing?


Developer time investment Drivers and stubs

Automation of testing process


Enhanced test coverage Smallest piece of testable software in an application

Helps to identify the majority of defects

Steps to Creating a Unit Test


Create the code that is to be tested Use the Create Unit Test feature in Visual Studio

Create a Unit Test

Generate the skeletons for the Unit Test

Add validation

Demonstration: Creating and Running a Unit Test

Open existing solution Open code file

Modify property
Create unit test Run test

Optional Unit Test Steps


Create a test category Create a test list

View code coverage

Lesson 3: Processing Unhandled Exceptions


Displaying a custom error page Processing unhandled exceptions

Displaying a Custom Error Page


Runtime errors are unavoidable Unhandled errors raises HttpApplication.Error event

Configure user-friendly custom error page

<system.web> <customErrors mode="RemoteOnly" defaultRedirect="~/CustomErrorPage.aspx" /> ... </system.web>

Processing Unhandled Exceptions


HttpApplication.Error event can be placed in Global.asax

file

[Visual C#] protected void Application_Error(object sender, EventArgs e) { // Code that runs when an unhandled error occurs }

[Visual Basic] Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs) ' Fires when an error occurs End Sub

Lesson 4: Test Driven Development


Advantages of test-driven development (TDD) over

traditional development

The test-driven development process

Advantages of Test-Driven Development over Traditional Development


Test-driven development promotes loosely coupled and

highly cohesive code.

The test suite acts as documentation for the functional

specification of the final system.

The system uses automated tests, which significantly

reduce the time taken to retest the existing functionality for each new build of the system. you must perform to resolve the problem. You also have a clear measure of success when the test no longer fails. This increases your confidence that the system actually meets the customer requirements.

When a test fails, you have a clear idea of the tasks that

The Test-Driven Development Process


Create the test code Write or modify the functional code

Create additional tests


Test the functional code Refactor the code

Lab: Debugging, Unit Testing and Refactoring


Exercise 1: Configuring Error Handling Exercise 2: Debugging Code

Exercise 3: Logging
Exercise 4: Creating Unit Tests Exercise 5: Implementing the Test-First/Test-Driven

Development Methodology

Logon information

Virtual machine User name Password

10264A-GEN-DEV Student Pa$$w0rd

Estimated time: 60 minutes

Lab Scenario

Lab Review
Why was the generic error handler implemented in the

Global.asax file?

Where is a custom error page configured? What are the advantages and disadvantages of using the

Event Log for logging errors?

Module Review and Takeaways


Review Questions Common Issues and Troubleshooting Tips

Real-world Issues and Scenarios


Best Practices Tools

Вам также может понравиться