控制QUnit测试顺序

时间:2011-02-07 16:33:12

标签: jquery ajax unit-testing qunit

我已经使用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个测试级联起来,但我认为它的可读性会降低......

您怎么看?

3 个答案:

答案 0 :(得分:8)

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

QUnit.config.reorder = false;

答案 1 :(得分: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();
  });
});

答案 2 :(得分:-4)

正如凯洛蒂所说,qUnit用于单元测试,“单元测试应该是独立的,彼此隔离”...... 然后我必须在测试删除之前添加一个元素。