Skip to content

Instantly share code, notes, and snippets.

@amazedkoumei
Created May 1, 2012 00:31
Show Gist options
  • Save amazedkoumei/2563998 to your computer and use it in GitHub Desktop.
Save amazedkoumei/2563998 to your computer and use it in GitHub Desktop.
qunit and mockjax sample
/*
* コメントの数字は実行される順序です。
* asyncTestメソッドはstart()メソッドが実行されるまで次のテストの実行を待機します。
*/
asyncTest("test1",function(){ // 1
doSomething(); // 4
start();
});
asyncTest("test2",function(){ // 2
start();
doSomething(); // 5 or 6
});
asyncTest("test3",function(){ // 3
doSomething(); // 5 or 6
start();
});
test("normal test",function(){
ok(true, "trueならGreen");
equal(1, "1", "1=='1'");
deepEqual(1, 1, "1===1");
});
$(function(){
module("function test");
test("changePxInt function", function(){
var test1 = changePxInt("20px");
equal(20, test1, "normal test");
var test2 = changePxInt("0px");
equal(0, test2, "normal test not number");
var test3 = changePxInt("px");
equal(0, test3, "abnormal test bad string px");
var test4 = changePxInt("20p");
equal(0, test4, "abnormal test bad string 20p");
var test5 = changePxInt("");
equal(0, test5, "abnormal test empty");
});
module("event test");
$(function(){
test("hover", function(){
var img = $("<img>").attr("src", "test.jpg").imgOver("_o");
//testを評価するfunction
var over_handler = function(event, data){
equal("test_o.jpg" , img.attr("src"), "normal test mouse over");
};
var out_handler = function(event, data){
equal("test.jpg" , img.attr("src"), "normal test mouse out");
};
//bindで評価するfunctionを渡して、
//triggerでイベントを呼ぶ
img.bind("mouseover", over_handler)
.trigger("mouseover")
.unbind("mouseover", over_handler);
img.bind("mouseout", out_handler)
.trigger("mouseout")
.unbind("mouseout", out_handler);
});
});
module("ajax test");
//モックを作成
var logoutMock = $.mockjax({
url: '/restful/fortune',
responseTime: 750,
responseText: {
login: 0
}
});
//処理の実行
var fortune = getFortune();
stop();
setTimeout(function(){
start();
//評価
notEqual(fortune,'Things do not look good, no fortune was told', 'サーバーから結果を取得する');
//mockのお片づけ
$.mockjaxClear(id);
}, 500);
});});
/*
* コメントの数字は実行される順序です。
* testメソッドに与えられたfunctionは非同期に実行されます。
*/
test("normal test",function(){ // 1
doSomething(); // 3 or 4
});
test("normal test",function(){ // 2
doSomething(); // 3 or 4
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment