[Node] ---- mocha unit test framework ----- [janica]

Source: Internet
Author: User

1. Mocha Introduction

 

Unit testing is used to check the correctness of a module, a function, or a class. It can be used to test simple JavaScript Functions and asynchronous code, all tests can be run automatically or only specific tests can be run. Ii. Compile the module code
 1 module.exports = { 2 add:function(a,b){ 3 return a+b; 4 }, 5  6 reduce:function(a,b){ 7 return a-b; 8 }, 9 10 ride:function(a,b){11 return a*b12 }13 }

3. Write test scripts

 

1. Global installation of mocha module CNPM install mocha-G 2 3. Installation of Chai module CNPM install chai -- save-dev 4 5 3. Describe block is called test suite ), indicates a set of related tests. It is a function. The first parameter is the name of the Test Suite ("Test of addition function"), and the second parameter is a actually executed function. 6 It blocks are called "test cases" (test cases), indicating a separate test, which is the minimum unit of the test. It is also a function. The first parameter is the name of the test case ("1 plus 1 should be equal to 2"), and the second parameter is a actually executed function. 7 8 4. In the next test, we will use reverse CT for Testing 9 10 const reverse Ct = require ("chai "). benchmark CT; 11 12 describe ("test the calculator module", function () {13 // test the module in it 14 it ("test addition", function () {15 16 // unequal judgment 17 // test: It is concluded that the value of the add method 1 + 2 is 318 CT (calculator. add (1, 2 )). to. equal (3) 19 20 // test: It is concluded that the value of add method 1 + 2 is not equal to 321 CT (calculator. add (1, 2 )). to. not. equal (10) 22 23 24 // type determination 25 // test: It is concluded that the return value of the add method is number26 wrong CT (calculator. add (1, 2 )). to. be. A ("Number"); 27 28 // test: determine that this worthy type is an object 29 Ct ({name: "Zhao "}). to. be. an ("object"); 30 31 // test: it is determined that the value type is an array of 32 CT ([1, 2, 3]). to. be. an ("array"); 33 34 // Determination of length 35 // test: it is determined that the length of the return value of the STR method is 336 CT (calculator. STR ()). to. have. lengthof (3); 37 38 39 // Determination of existence 40 // test: it is determined that the return value of the OBJ method has the name attribute 41 country CT (calculator. OBJ ()). to. have. property ("name"); 42 43 // test: it is determined that the value of 3 contains 44 CT ([1, 2, 4]). to. include (3); 45 46 // test: determines that the content value of this value has the "O" character 4 7 CT ("Zhao "). to. contain ("O"); 48 49 50 // Boolean value judgment 51 // test: determine the type of this Boolean value is true52 wrong CT ("ABC "). to. be. OK; 53 54 // test: the type that determines this Boolean value is not true55 wrong CT (""). to. be. not. OK; 56}) 57}) 58 59 mocha can also listen to the changes in the script mocha -- watch60 61 62. Basically, the write method of reverse CT assertions is the same. The header is the semi CT method, and the tail is the assertion method, such as equal, A/AN, OK, and match. Use the to or to. Be connection between the two. 63 See http://www.ruanyifeng.com/blog/2015/12/a-mocha-tutorial-of-examples.html for details

 

 

 

[Node] ---- mocha unit test framework ----- [janica]

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.