简体   繁体   English

使用Ajax进行QUnit,QUnit会通过失败的测试

[英]QUnit with Ajax, QUnit passes the failing tests

I am looking into QUnit for JavaScript unit testing. 我正在研究QUnit for JavaScript单元测试。 I am in a strange situation where I am checking against the value returned from the Ajax call. 我在一个奇怪的情况下,我正在检查从Ajax调用返回的值。

For the following test I am purposely trying to fail it. 对于以下测试,我故意试图让它失败。

// test to check if the persons are returned! 
test("getPersons", function() {
  getPersons(function(response) {
    // persons = $.evalJSON(response.d);
    equals("boo", "Foo", "The name is valid");
  });
});

But it ends up passing all the time. 但它最终总是在传递。 Here is the getPersons method that make the Ajax call. 这是进行Ajax调用的getPersons方法。

function getPersons(callback) {
  var persons = null;

  $.ajax({
    type: "POST",
    dataType: "json",
    data: {},
    contentType: "application/json",
    url: "AjaxService.asmx/GetPersons",
    success: function(response) {
      callback(response);
    }
  });
}

Starting and stopping using the QUnit library seems to be working! 使用QUnit库启动和停止似乎正在运行!

// test to check if the persons are returned!
test("getPersons", function() {
  stop();
  getPersons(function(response) {
    persons = $.evalJSON(response.d);
    equals(persons[0].FirstName, "Mohammad");
    start();
  });
});

The real problem here isn't needing to call the start() and stop() methods - in fact you could get into trouble using that approach if you are not careful in calling stop() again at the end of your callback if you have additional .ajax() methods. 这里真正的问题是不需要调用start()和stop()方法 - 事实上,如果你不小心在回调结束时再次调用stop(),你可能会遇到麻烦额外的.ajax()方法。 Which then means you find yourself in some snarled mess of having to keep track if all the callbacks have been fired to know if you still need to call stop() again. 这意味着你发现自己处于一些混乱的状态,如果所有的回调都被触发,你必须跟踪,以了解你是否还需要再次调用stop()。

The root of the problem involves the default behavior of asynchronous requests - and the simple solution is to make the .ajax() request happen synchronously by setting the async property to false: 问题的根源涉及异步请求的默认行为 - 简单的解决方案是通过将async属性设置为false来使.ajax()请求同步发生:

test("synchronous test", function() {
  $.ajax({
    url:'Sample.asmx/Service',
    async:false,
    success: function(d,s,x) { ok(true, "Called synchronously"); }
  });
});

Even still, the best approach is to allow the asynchronous behavior and use the right test method invocation: asyncTest() . 即便如此,最好的方法是允许异步行为并使用正确的测试方法调用: asyncTest() According to the docs " Asynchronous tests are queued and run one after the other. Equivalent to calling a normal test() and immediately calling stop(). " 根据文档“ 异步测试排队并一个接一个地运行。相当于调用正常测试()并立即调用stop()。

asyncTest("a test", function() {
  $.ajax({
    url: 'Sample.asmx/Service',
    success: function(d,s,x) {
      ok(true, "asynchronous PASS!");
      start();
    }
  });
});

There a lot of qunit test in my project. 我的项目中有很多qunit测试。 like: 喜欢:

    module("comment");
    asyncTest("comment1", function() {
      expect(6);
      $.ajax({
        url: 'http://xxx.com/comment',
        dataType: "json",
        type: "GET",
        timeout: 1000
      }).done(function(data) {
        ok(true, "loaded");
        ok(data.data.length>1, "array size");
        ok(data.total, "attr total");
        var c = data.data[0];
        ok(c.id, "attr c.id");
        ok(c.user_id, "attr c.user_id");
        ok(c.type == 4, "attr c.type equal 4");
      }).fail(function(x, text, thrown) {
        ok(false, "ajax failed: " + text);
      }).always(function(){
        start();
      });
    });

ive done some qunit testing with ajax. 我用ajax做了一些qunit测试。 its not pretty. 它不漂亮。 the best thing i could come with is stopping the test when ajax is fired, and starting it again in the success callback. 我可以带来的最好的事情是在ajax被触发时停止测试,并在成功回调中再次启动它。 (using start() and stop()) methods. (使用start()和stop())方法。 This meant one ajax request at a time, but i could live with that. 这意味着一次一个ajax请求,但我可以忍受。 Good luck 祝好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM