JavaScript refactoring (III): JavaScript Testing

Source: Internet
Author: User

During JavaScript refactoring, I hope to introduce an easy-to-use testing framework to ensure smooth refactoring. In the future, I will continue to use the testing code to ensure the correctness of the JavaScript logic.
Http://sourceforge.net/projects/jsunit/,http://www.jsunit.net/ (JsUnit)
JsUnit is an independent JavaScript unit testing framework, which is similar to JUnit and has no difficulty in getting started, including the traditional setUp and tearDown methods. The assert method provided is similar to JUnit, besides the JavaScript-specific methods such as assertNaN and assertUndefined. The jsUnitCore. js file must be introduced to the Test Suite support: Provides addTestPage and addTestSuite;
Test log support: it includes three log levels: warn, info, and debug. The front-end code is not similar to the background code. Too many logs should not be used in the official code. Besides, log is only supported under FF.
 
The following is an example:
Js Code
<Script language = "javascript" src = "jsUnitCore. js"> </script>
<Script language = "javascript" src = "play. js"> </script> // module JS
 
Function testWithMainProcess (){
AssertEquals ("Web play url", "# http: //... #", webOnlinePlay ());
}
 
Ajax calls are everywhere in the project code. To perform unit tests, it seems that piling is inevitable. There are many Mock tools, such as QMock suitable for JQuery:
Js Code
Var mockJquery = new Mock ();
MockJquery
. Expects (1)
. Method ('ajax ')
. WithArguments ({
Url: 'http: // xxx,
Success: Function,
DataType: "jsonp"
})
. CallFunctionWith ({feed: {entry: "data response "}});
 
This is exactly what mock returned by a fake ajax jason: [feed: [entry: "data response"]. You can see that it is similar to EasyMock.
 
If you are interested in the JavaScript testing framework, you can also learn about other testing frameworks, such as JSpec.
 
It is recommended that the unit test code be placed in the module package: test.html. Even if the module is released independently under ideal conditions, it is also a reliable front-end code that is accompanied by the test case.
What JavaScript code does it start?
1. Functional code. This Code ensures good independence and does not need to be written. The test cost is low. If you do not understand the meaning of function code, see "function programming ".
2. complicated logic.
Try TDD? We do not recommend using it within our team. Front-end TDD requires higher skills and higher requirements on human factors. If one day, the tdd of the background Java code is ready, there is no essential difference between the code changed to JavaScript.
 
If it works well, why not integrate JavaScript UT into the ICP-CI as part of continuous integration?

Author's "four-fire BLOG"
 

Related Article

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.