注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

星期五

明天不上班

 
 
 

日志

 
 
关于我

一个特立独行的Java程序员,比较宅,上上网,写博客,听音乐,看电影。

网易考拉推荐

如何书写dojo UT  

2014-03-09 17:18:30|  分类: Javascript |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
如何书写dojo UT,以array.js为例 (array.js src code

目录结构

-- dojo-release-1.9.1-src
-- my_toolkit
-- my
-- array.js

-- fx.js

 -- tests

-- fx.js

-- fx.html

 -- array.js
-- testAll.js

在tests中的目录书写对应的array.js, 这个文件用于测试

define(["doh", "my/array", "dojo/_base/lang"], function(doh, array, lang){

doh.register("tests.array", [
function testUniq(t){
var foo = [128, 128, 256, 512];
var bar = ["aaa", "bbb", "ccc", "ccc", "ccc"];

t.assertEqual([128, 256, 512], array.uniq(foo));
t.assertEqual(["aaa", "bbb", "ccc"], array.uniq(bar));
},

function testUnion(t){
var foo1 = [128, 128, 256, 512];
var foo2 = [128, 256, 333, 444];
var bar1 = ["aaa", "bbb", "ccc"];
var bar2 = ["aaa", "ddd"];

t.assertEqual([128, 256, 512, 333, 444], array.union(foo1, foo2));
t.assertEqual(["aaa", "bbb", "ccc", "ddd"], array.union(bar1, bar2));
},


function testIntersection(t){
var foo1 = [128, 128, 256, 512];
var foo2 = [128, 256, 333, 444];
var bar1 = ["aaa", "bbb", "ccc"];
var bar2 = ["aaa", "ddd"];
t.assertEqual([128, 256], array.intersection(foo1, foo2));
t.assertEqual(["aaa"], array.intersection(bar1, bar2));
},

function testDifference(t){
var foo1 = [128, 128, 256, 512];
var foo2 = [128, 256, 333, 444];
var bar1 = ["aaa", "bbb", "ccc"];
var bar2 = ["aaa", "ddd"];
t.assertEqual([512], array.difference(foo1, foo2));
t.assertEqual(["bbb","ccc"], array.difference(bar1, bar2));
},
]);
});

在testAll.js中导入array.js Test

define([
"my/tests/array"
], 1);

有时候测试会用到html元素,这时候就需要些html页面。
以fx.js测试为例,书写fx.html (这个写法可以参考dojo/tests/fx.html)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Testing fx</title>
<script>

dojoConfig = {
async: true,
tlmSibingOfDojo: false,
parseOnLoad: true,
//isDebug: false,
locale: 'en-us',
baseUrl: "../../../dojo-release-1.9.1-src/",
packages: [
{name: "dojo", location: "./dojo"},
{name: "dijit", location: "./dijit"},
{name: "dojox", location: "./dojox"},
{name: "gridx", location: "./gridx"},
{name: "doh", location: "./util/doh"},
{name: "idx", location: "../idx_toolkit/idx"},
{name: "cuf", location: "../cuf_toolkit/cuf"},
{name: "rbac", location: "../cuf_toolkit/rbac"}
]
};
</script>
<script type="text/javascript" src="http://zhenghaoju700.blog.163.com/../../../../dojo-release-1.9.1-src/dojo/dojo.js" data-dojo-config="isDebug:true"></script>
<script type="text/javascript">

require([
"doh",
"dojo/_base/array", "dojo/aspect", "dojo/dom", "dojo/dom-style","cuf/fx",
"dojo/domReady!"
], function(doh, array, aspect, dom, domStyle, fx){
doh.register("t",
[
function hWipeOut(t){
var d = new doh.Deferred();
var s = fx.hWipeOut({
node: "foo",
duration: 10,
onEnd: function(){
console.log(domStyle.get("foo", "width"));
doh.t(domStyle.get("foo", "width") < 10);
d.callback(true);
}
}).play();
return d;
},

function hWipeIn(t){
var d = new doh.Deferred();

setTimeout(function(){
fx.hWipeIn({
node: "foo",
duration: 10,
onEnd: function(){
console.log(domStyle.get("foo", "width"));
doh.t(domStyle.get("foo", "width") > 10);
d.callback(true);
}
}).play();
}, 10);
return d;
},
{
name: "hWipeOut onStop",
timeout: 1500,
runTest: function(t){
dom.byId("foo").style.width = "auto";
dom.byId("foo").style.overflow = "visible";
var d = new doh.Deferred();
var w = fx.hWipeOut({
node: "foo",
duration: 1000
});
aspect.after(w, "onStop", function(){
doh.t(dom.byId("foo").style.overflow == "visible");
d.callback(true);
}, true);
w.play();
setTimeout(function(){ w.stop(); }, 100);
return d;
}
},

{
name: "hWipeIn onStop",
timeout: 1500,
runTest: function(t){
dom.byId("foo").style.width = "0px";
dom.byId("foo").style.overflow = "visible";
var d = new doh.Deferred();
var w = fx.hWipeIn({
node: "foo",
duration: 1000
});
aspect.after(w, "onStop", function(){
doh.t(dom.byId("foo").style.overflow == "visible");
d.callback(true);
}, true);
w.play();
setTimeout(function(){ w.stop(); }, 100);
return d;
}
}
]
);
doh.run();
});
</script>
<style type="text/css">
@import "../../../dojo-release-1.9.1-src/dojo/resources/dojo.css";

body {
text-shadow: 0px 0px;
margin: 1em;
background-color: #DEDEDE;
}

.box {

color: #292929;
/* color: #424242; */
/* text-align: left; */
border: 1px solid #BABABA;
background-color: white;
margin-left: 10px;
margin-bottom: 1em;
-o-border-radius: 10px;
-moz-border-radius: 12px;
-webkit-border-radius: 10px;
-webkit-box-shadow: 0px 3px 7px #adadad;
/* -opera-border-radius: 10px; */
border-radius: 10px;
-moz-box-sizing: border-box;
-opera-sizing: border-box;
-webkit-box-sizing: border-box;
-khtml-box-sizing: border-box;
box-sizing: border-box;
overflow: hidden;
/* position: absolute; */
}
</style>
</head>
<body>
<div id="foo" class="box">
<p>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean
semper sagittis velit. Cras in mi. Duis porta mauris ut ligula.
Proin porta rutrum lacus. Etiam consequat scelerisque quam. Nulla
facilisi. Maecenas luctus venenatis nulla. In sit amet dui non mi
semper iaculis. Sed molestie tortor at ipsum. Morbi dictum rutrum
magna. Sed vitae risus.
</p>
<p>
Aliquam vitae enim. Duis scelerisque metus auctor est venenatis
imperdiet. Fusce dignissim porta augue. Nulla vestibulum. Integer
lorem nunc, ullamcorper a, commodo ac, malesuada sed, dolor. Aenean
id mi in massa bibendum suscipit. Integer eros. Nullam suscipit
mauris. In pellentesque. Mauris ipsum est, pharetra semper,
pharetra in, viverra quis, tellus. Etiam purus. Quisque egestas,
tortor ac cursus lacinia, felis leo adipiscing nisi, et rhoncus
elit dolor eget eros. Fusce ut quam. Suspendisse eleifend leo vitae
ligula. Nulla facilisi. Nulla rutrum, erat vitae lacinia dictum,
pede purus imperdiet lacus, ut semper velit ante id metus. Praesent
massa dolor, porttitor sed, pulvinar in, consequat ut, leo. Nullam
nec est. Aenean id risus blandit tortor pharetra congue.
Suspendisse pulvinar.

</p>
</div>
</body>
</html>

接着书写fx.js

define(["doh/main", "require"], function(doh, require){
if(doh.isBrowser){
doh.register("tests.fx", require.toUrl("./fx.html"), 30000);
}
});

将fx.js 导入到testAll.js

define([
"cuf/tests/array",
"cuf/tests/fx"
], 1);

执行testAll测试查看结果
如何书写dojo UT - 星期五 - 星期五

从上面连个UT总结一下dojo UT的写法,使用过JUnit的同学应该会比较容易理解
两种写法:
第一种简单写法,就是一个function函数,像在array.js中写的测试
第二种写法比较复杂点,是一个测试对象,它包含以下property:
name: 测试名称

setUp函数: 在runTest之前执行,一般用于为测试准备实例等等一些初始条件
runTest函数: 测试函数
tearDown:  在runTest之后执行,一般进行测试之后的善后工作,例如销毁没有用的对象等
timeout: 超时时间

{
  name: "thingerTest",
  setUp: function(){
    // Setup to do before runTest.
    this.thingerToTest = new Thinger();
    this.thingerToTest.doStuffToInit();
  },
  runTest: function(){
    // Our test function to run.
    doh.assertEqual("blah", this.thingerToTest.blahProp);
    doh.assertFalse(this.thingerToTest.falseProp);
    // ...
  },
  tearDown: function(){
    // cleanup to do after runTest.
  },
  timeout: 3000 // 3 second timeout.
}
另外,还有一点,如何进行异步测试,这个fx.html中有写法,这里就不举例了。

参考资料:
http://dojotoolkit.org/reference-guide/1.9/util/doh.html

  评论这张
 
阅读(387)| 评论(0)
推荐

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017