jQuery と多数の ajax リクエスト (json 形式) を使用して Web サイトを構築しました。
サーバー側でリクエストを検証するための単体テストを行いたいと思います。
jQueryを使っていたのでqUnitを使っているのですが、テストの順番に問題が...
たとえば、これをテストしたかった: - ユーザーの作成 => 可能
- 有効な名前でユーザーの名前を変更 => 可能
- 使用済みの名前でユーザーの名前を変更 => 不可能
- ユーザーを削除 = >ありえるかも
私のコード:
$("button#test").button().click(function() {
module("Module Users");
newName = 'newUserName';
userId = 0;
test("1 Add a user", function() {
stop();
$.getJSON(Request,{'action':'add','table':'users'}
,function(data) {
equal( data.status,"OK", "Answer is OK" );
notEqual( data.item,null, "item is return" );
userId = data.item.id;
start();
});
});
test("2 Rename user", function() {
stop();
$.getJSON(Request,{'action':'modify','table':'users','id':userId,'field':'name','value':newName}
,function(data) {
equal( data.status,"OK", "Answer is OK" );
equal( data.value,newName, "Return value is OK" );
start();
});
});
test("3 Rename user with use name", function() {
stop();
badName = 'usedName'; // assert that a user with this name exists
$.getJSON(Request,{'action':'modify','table':'users','id':userId,'field':'name','value':badName}
,function(data) {
equal( data.status,"Fail", "Answer is Fail" );
equal( data.value,newName, "Return value is previous name" );
start();
});
});
test("4 Remove the user", function() {
stop();
$.getJSON(Request,{'action':'remove','table':'users','id':userId}
,function(data) {
equal( data.status,"OK", "Answer is OK" );
start();
});
});
しかし、問題は 1 のテストが実行され、次に 4 と 2 と 3 が実行されることです... (そして、問題は私のテストが独立していないことだと思います)
これをどのように解決できますか?
4つのテストすべてを1つにカスケードできますが、読みにくくなると思います...
どう思いますか ?