首页 技术 正文
技术 2022年11月9日
0 收藏 847 点赞 2,749 浏览 3777 个字

<!–
.Abstract {
padding: 15px;
border: dotted 2px #999;
color: #999;
font-family: ‘Microsoft Yahei’;
border-radius: 4px;
}
.First {
margin: 10px 0;
font-family: ‘Microsoft Yahei’;
text-align: left;
padding: 6px 20px;
color: #fff;
background: #55895B;
font-size: 20px;
border-radius: 4px;
clear: both;
}
.Second {
margin: 10px 0;
font-family: ‘Microsoft Yahei’;
padding: 6px 20px;
background: #93C8A2;
color: white;
font-size: 18px;
border-radius: 4px;
clear: both;
}
.Third {
margin: 10px 0;
padding: 6px 20px;
font-family: ‘Microsoft Yahei’;
margin: 15px 0;
font-size: 16px;
color: black;
background: #C6EFD2;
border-radius: 4px;
clear: both;
}
.note {
margin: 10px 0;
padding: 15px 20px 15px 60px;
background: #FCFAA9 url(‘http://images.cnblogs.com/cnblogs_com/libaoheng/305804/o_yellow-pin.png’) no-repeat 20px 0;
font-size: 15px;
font-family: ‘Microsoft Yahei’;
box-shadow: 0 0 8px #aaa;
clear: both;
}
.demo {
text-align: left;
padding: 6px 20px;
overflow: auto;
border-radius: 4px;
background: orange;
color: #fff;
font-size: 16px;
clear: both;
}
.cnblogs_Highlighter {
border: solid 1px #ccc;
clear: both;
}
.cnblogs_code {
background: #EFFFF4;
border: solid 0px #939393;
font-size: 14px;
clear: both;
padding: 10px 20px;
}
.cnblogs_code pre {
font-size: 14px;
}
.cnblogs_code span {
font-family: Courier New;
font-size: 14px;
}
–>

JS单元测试,我工作时主要以mocha + chai 下面时具体的文档:

mocha:

  https://mochajs.org/#asynchronous-code

Chai:

  http://chaijs.com/api/bdd/#method_ownproperty

还是比较简单的所以可以自己查看文档:

例子基于requirejs,并且配置需基于你自己文件目录,红色代码请留意下

要测试的一个排序函数,对对象数组进行升、降序排序

define(['underscore'], function(_) {
function util() {};
//对象数组,根据item中某字段排序
util.sortBy = function () {
var obj = arguments[0], key = arguments[1];
if(!_.isArray(obj) || !key) {
return obj;
}
var sort = arguments[2], j = 1, temp = {};
//交换函数
function swap (obj, index) {
temp = obj[index];
obj[index] = obj[index+1];
obj[index+1] = temp;
}
//sort 默认从小到大 (采用冒泡)
obj.forEach(function() {
for(var i = 0, length = obj.length-j; i < length; i++) {
if (sort && sort.toLowerCase() ==="desc") { //降序
if (!obj[i][key]) {
if (obj[i+1][key]) {
swap(obj, i);
}
} else if (obj[i+1][key] && obj[i+1][key] > obj[i][key]) {
swap(obj, i);
}
} else { //默认升序
if (obj[i][key]) {
if (obj[i+1][key]) {
if(obj[i][key] > obj[i+1][key]){
swap(obj, i);
}
} else {
swap(obj, i);
}
}
}
}
j++;
});
return obj;
} return util;
})

测试页面代码,具体的配置需要根据你自己的环境去配置

<!DOCTYPE html>
<html>
<head>
<title>H5 test</title>
<link rel="stylesheet" type="text/css" href="../../node_modules/mocha/mocha.css" rel="external nofollow" >
<script src="../asset/src/h5/vendor/require.js">
</script>
</head>
<body>
<div id="mocha"></div>
</body>
<script type="text/javascript">
(function(){
function nodePath(path) {
return '../../node_modules/' + path + '/' + path;
}; function modulePath(path) {
return './lib/' + path;
};
function vendorPath(path) {
return '../asset/src/h5/vendor/' +path;
};
function widgetPath(path) {
return '../asset/src/widget/' + path;
}; requirejs.config({
urlArgs: Date.now(),
baseUrl: './',
shim: {
mocha: {
exports: 'mocha'
}
},
paths: {
'underscore': vendorPath('underscore'),
'mocha': nodePath('mocha'),
'chai': nodePath('chai'),
'testUtil': modulePath('testUtil'),
'util': widgetPath('util')
}
}); requirejs(['mocha', 'chai'], function(mocha, chai) {
mocha.setup('bdd'); window.expect = chai.expect; //加载测试模块
require(['testUtil'], function() { if(window.mochaPhantomJS) {
window.mochaPhantomJS.run();
} else {
mocha.run();
}
})
});
})();
</script>
</html>

具体的测试代码:

describe('util', function(util) {
var util;
before(function(done){
require(['util'], function(one) {
util = one;
done();
});
}) describe('#sortBy', function() {
it('obj类型数组asc排序', function() {
//expect(util.sortBy([{a:1, b: 1}, {a:4, b:5}, {a:3}, {a:2}, {b:2}, {a:5}], 'a') == [{b:2}, {a:1, b:1}, {a:2}, {a:3}, {a:4, b:5}, {a:5}]).to.be.true;;
var str='';
util.sortBy([{a:1, b: 1}, {a:4, b:5}, {a:3}, {a:2}, {b:2}, {a:5}], 'a').forEach(function(item) {
str = str + item.a +",";
}) expect(str === "undefined,1,2,3,4,5,").to.be.true;
}) it('obj类型数组desc排序', function() {
var str='';
util.sortBy([{a:1, b: 1}, {a:4, b:5}, {a:3}, {a:2}, {b:2}, {a:5}], 'a', 'desc').forEach(function(item) {
str = str + item.a +",";
}) expect(str ==="5,4,3,2,1,undefined,").to.true;
}); it('ddd', function() {
expect(1==2).to.true;
})
});
});

运行结果图

JS 单元测试

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,023
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,513
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,360
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,143
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,774
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,853