3

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つにカスケードできますが、読みにくくなると思います...

どう思いますか ?

4

3 に答える 3

8

必要に応じて、仕事を終わらせたいだけの場合もあります。

QUnit.config.reorder = false;

于 2012-08-14T02:37:03.813 に答える
1

この例の主な問題は、テストがクリック イベント ハンドラー内で実行されることです。それをリファクタリングし、最上位で test() を呼び出す必要があります (クリック イベントとは無関係です)。テスト自体は ajaxy 機能のみをテストするため、ボタンを使用する必要はまったくありません。だから、このようなもの:

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();
  });
});
于 2011-02-26T13:43:26.313 に答える
-4

kelloti が言うように、qUnit は単体テスト用であり、「単体テストは独立しており、互いに分離されているはずです」...次に、削除をテストする前に要素を追加する必要があります。

于 2011-03-03T21:25:20.850 に答える