利用should.js进行测试
2016-03-22 15:26
477 查看
nodejs 环境 , 安装should.js包 (npm install should)
mocha + should (npm install -g mocha)
var should = require('should'); //正确1, 错误0 100 precentNum2(1,0).should.eql(100); //正确0, 错误1 0 precentNum2(0,1).should.eql(0); //正确0, 错误0 -- precentNum2(0,0).should.eql('- -'); //正确1, 错误1 50 precentNum2(1,1).should.eql(50); //正确1, 错误3 25 precentNum2(1,3).should.eql(25); //正确1, 错误2 33 precentNum2(1,2).should.eql(33); //正确1, 错误2 33 precentNum2(1,4).should.eql(33); function precentNum2(num, num2) { var correctQuestions = parseInt(num); var wrongQuestions = parseInt(num2); if((correctQuestions + wrongQuestions) === 0) { return "- -"; } else if(correctQuestions === (correctQuestions + wrongQuestions)) { return 100; } else if(correctQuestions === 0 && wrongQuestions !== 0) { return 0; } else { var temp = correctQuestions / (correctQuestions + wrongQuestions); var tempResult = parseInt(temp.toFixed(2).substr(2,2)); if(tempResult > 0 && tempResult < 101) { return tempResult; } else { return "- -"; } } }
mocha + should (npm install -g mocha)
var should = require('should'); describe('Precent', function(){ describe('Precent Test', function(){ it('Test precentNum2', function(){ //正确1, 错误0 100 precentNum2(1,0).should.eql(100); //正确0, 错误1 0 precentNum2(0,1).should.eql(0); //正确0, 错误0 -- precentNum2(0,0).should.eql('- -'); //正确1, 错误1 50 precentNum2(1,1).should.eql(50); //正确1, 错误3 25 precentNum2(1,3).should.eql(25); //正确1, 错误2 33 precentNum2(1,2).should.eql(33); //正确1, 错误2 33 precentNum2(1,4).should.eql(20); }) }) }); function precentNum2(num, num2) { var correctQuestions = parseInt(num); var wrongQuestions = parseInt(num2); if((correctQuestions + wrongQuestions) === 0) { return "- -"; } else if(correctQuestions === (correctQuestions + wrongQuestions)) { return 100; } else if(correctQuestions === 0 && wrongQuestions !== 0) { return 0; } else { var temp = correctQuestions / (correctQuestions + wrongQuestions); var tempResult = parseInt(temp.toFixed(2).substr(2,2)); if(tempResult > 0 && tempResult < 101) { return tempResult; } else { return "- -"; } } }
相关文章推荐
- 关于如何在jsp通过javascript获取表单中的值,以及为什么登陆界面刷新以后图片都不见了得原因
- JS的永恒闭包问题
- jsp相关--基础
- 再谈javascript中的闭包
- js中判断两个对象
- js中cookie的学习与使用
- seajs 入门
- Javascript中Base64编码解码的使用实例
- js中如何定义常量
- jsapi 调用创建企业会话和企业通讯录
- Javascript中this关键字详解
- 深入理解js构造函数
- jstl 的if else <c:choose><c:when test="">如果</c:when><c:otherwise>否则</c:otherwise></c:choose>
- ExtJs之Element.select函数
- javascript中的__proto__, prototype和constructor
- jspIO产生的异常
- JS的匿名函数
- js里父页面与子页面的相互调用(2012-05-22 09:58:45)转载▼
- jsp界面上日期格式的处理
- jsp错误页面导航error page的使用