Lecture 12th: Test-driven development in MVC

Source: Internet
Author: User

2010.9.12 su Peng

Content

-Test-driven development

-Use TDD for each layer of ASP. Net MVC

 

Prerequisites

-Install Visual Studio 2010 Professional

-Understand ASP. Net

-Basic knowledge of TDD

-Understand the basic concepts of the Design Mode

 

The origin of TDD

-It is a methodology for implementing code, not a testing method.

In fact, it is a development method to ensure the construction of software code and the quality of software.

 

What is TDD

Simply put, TDD is a process of testing first, adding refactoring, and then regression testing.

To implement a function, TDD requires that the test code be written according to the use case of the project after the external interface of the function is initially defined. The test code verifies the use scenario of the external interface of the project, rather than the specific implementation details, that is, the black box test. Then there is the external interface that implements this function. In the implementation process, write some tests as needed. Generally, unit tests are not only for interfaces, but also for interface implementation details. These are white-box tests.

If you find that the external interface is designed incorrectly, You need to modify and refactor it. After each code modification, or before adding new features, all existing tests need to be run again. Test first ensures the logic and completeness of all application scenarios, rather than the optimization and efficiency of algorithms. Reconstruction aims to improve the design and implementation as much as possible while correcting the Functional Semantics. Regression testing ensures that any modifications do not destroy existing functions. In this way, developers can focus on implementing and improving their current functions with confidence and efficiency, and ensure code integrity and functional correctness at all times.

 

Why TDD?

-Code quality is more reliable

The biggest benefit of TDD is to ensure the security of any existing functions during each code addition and modification process. Especially when compilation and regression testing takes a short time.

Another benefit of TDD is its ability to better interpret system functions. In fact, each unit test is a function point. It should be regarded as a document that is well maintained and well understood.

 

How TDD works

-Write a unit test.

-Write code to pass the test

-Code Reconstruction

-Iteration

 

1. Write test code

 

2. Write application code

 

3. refactor the above Code

-Refactoring to streamline code

Refactoring has two meanings: sorting out existing functions to avoid repeated function classes; adjusting the structure of the Internal statements of a function to avoid too bloated functions.

 

4. Iteration

-Repeat the above process

 

Compiled unit test code

-Testing skills

 

-Do not cross-border

-Code testing helps you locate functions.

Do not involve multiple classes or methods in a unit test function.

 

-Default unit test

 

-Unit testing should be concise

-Only test the code written by yourself.

 

Benefits of using TDD

-Clear test objectives

-Docized Test

-Test Security

-Test to improve quality

 

Reference books for TDD recommendations

-Test Driven Development

-Test-Driven Development in Microsoft. NET

 

TDD of ASP. Net MVC

-Test Routes

-Test Controller

-Test View Helpers

-Test Views

 

-Testing Routes

-Moq http://code.google.com/p/moq

There is a problem here. To test Routes, you must test Action. This is inevitable. Therefore, if a problem occurs, check whether an error exists in the Action.

 

-Testing Controllers

 

-Testing View Helpers

This is a white box test. It is a detailed test. Although this is not in the scope of TDD, it is also necessary.

 

-Testing Views

-TDD testing is generally not performed.

The reason is that Views is a pure presentation layer. Presentation layer has three main features: response performance, compatibility, and availability. These three items determine whether your product can be used. These three aspects are not what we can do by writing a test in the background. Net. Different test methods are required for availability and compatibility. For example, for compatibility, you must at least simulate different browsers and present different Css. In addition, the Performance Tuning Method of JavaScript has been mentioned earlier. In fact, 90% of users think that poor web pages are caused by poor JavaScript writing, which leads to poor user experience. We recommend that you read the efficient JavaScript programming book.

In short, the Views layer does not need to write code tests in the background, but the front-end code tests are indispensable. It is necessary to ensure proper resolution of performance and compatibility issues.

 

Summary

-TDD Introduction

-Use TDD for each layer of ASP. Net MVC

2010.10.4

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.