利用should.js进行测试

nodejs 环境 , 安装should.js包 (npm install should)

 1 var should = require('should');
 2 
 3 //正确1, 错误0   100
 4 precentNum2(1,0).should.eql(100);
 5 //正确0, 错误1   0
 6 precentNum2(0,1).should.eql(0);
 7 //正确0, 错误0  -- 
 8 precentNum2(0,0).should.eql('- -');
 9 //正确1, 错误1  50
10 precentNum2(1,1).should.eql(50);
11 //正确1, 错误3  25 
12 precentNum2(1,3).should.eql(25);
13 //正确1, 错误2  33
14 precentNum2(1,2).should.eql(33);
15 //正确1, 错误2  33
16 precentNum2(1,4).should.eql(33);
17 
18 
19 
20 function precentNum2(num, num2) {
21     var correctQuestions = parseInt(num);
22     var wrongQuestions = parseInt(num2);
23 
24     if((correctQuestions + wrongQuestions) === 0) {
25         return  "- -";
26     } else if(correctQuestions === (correctQuestions + wrongQuestions)) {
27         return 100;
28     } else if(correctQuestions === 0 && wrongQuestions !== 0) {
29         return 0;
30     } else {
31         var temp = correctQuestions / (correctQuestions + wrongQuestions);
32         var tempResult = parseInt(temp.toFixed(2).substr(2,2));
33         if(tempResult > 0 && tempResult < 101) {
34             return tempResult;
35         } else {
36             return "- -";
37         }
38     }
39 }

mocha + should  (npm install  -g mocha)

 1 var should = require('should');
 2 describe('Precent', function(){
 3     describe('Precent Test', function(){
 4           it('Test precentNum2', function(){
 5             //正确1, 错误0   100
 6             precentNum2(1,0).should.eql(100);
 7             //正确0, 错误1   0
 8             precentNum2(0,1).should.eql(0);
 9             //正确0, 错误0  -- 
10             precentNum2(0,0).should.eql('- -');
11             //正确1, 错误1  50
12             precentNum2(1,1).should.eql(50);
13             //正确1, 错误3  25 
14             precentNum2(1,3).should.eql(25);
15             //正确1, 错误2  33
16             precentNum2(1,2).should.eql(33);
17             //正确1, 错误2  33
18             precentNum2(1,4).should.eql(20);
19         })
20     })
21 });
22 
23 function precentNum2(num, num2) {
24     var correctQuestions = parseInt(num);
25     var wrongQuestions = parseInt(num2);
26 
27     if((correctQuestions + wrongQuestions) === 0) {
28         return  "- -";
29     } else if(correctQuestions === (correctQuestions + wrongQuestions)) {
30         return 100;
31     } else if(correctQuestions === 0 && wrongQuestions !== 0) {
32         return 0;
33     } else {
34         var temp = correctQuestions / (correctQuestions + wrongQuestions);
35         var tempResult = parseInt(temp.toFixed(2).substr(2,2));
36         if(tempResult > 0 && tempResult < 101) {
37             return tempResult;
38         } else {
39             return "- -";
40         }
41     }
42 }
原文地址:https://www.cnblogs.com/maduar/p/5306677.html