3

我已经用 jQuery 和大量的 ajax 请求(json 格式)构建了一个网站。
我想做一些单元测试来验证服务器端的请求。
当我使用 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 ......(然后,我认为问题在于我的测试不是独立的)

怎么可能解决这个问题?
我可以在 1 中级联所有 4 个测试,但我认为它的可读性会降低......

你怎么看 ?

4

3 回答 3

8

有时您只是想完成工作如果需要,请尝试。

QUnit.config.reorder = false;

于 2012-08-14T02:37:03.813 回答
1

您的示例中的主要问题是测试在 click 事件处理程序中运行。您需要对其进行重构并在顶层调用 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 回答