本文介绍了我试图进行QUnit异步测试以检查Ajax更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图进行QUnit异步测试来检查ajax更新.

I tried to maka a QUnit async test for checking ajax update.

我在这里阅读了QUnit.asyncTest https://www.sitepoint.com/test-asynchronous-code-qunit/但是如果我尝试这个我会得到一个 TypeError:QUnit.asyncTest不是函数多数民众赞成在完整的来源: https://gist.github.com/232457b002e5363439aece7535600356

I read of QUnit.asyncTest herehttps://www.sitepoint.com/test-asynchronous-code-qunit/but if i try this i get aTypeError: QUnit.asyncTest is not a functionthats the complete source: https://gist.github.com/232457b002e5363439aece7535600356

我当然是通过使用QUnit而不是使用JavaScript来长期使用的.

of course i new by using QUnit and used JavaScript not for long time.

发生错误的部分的片段:

that a snippet of the part where the error happens:

function max() {
   var max = -Infinity;
   for (var i = 0; i < arguments.length; i++) {
      if (arguments[i] > max) {
         max = arguments[i];
      }
   }

   return max;
}
//   https://www.sitepoint.com/test-asynchronous-code-qunit/
//TypeError: QUnit.asyncTest is not a function
QUnit.asyncTest('max', function (assert) {
   expect(1);

   window.setTimeout(function() {
      assert.strictEqual(max(3, 1, 2), 3, 'All positive numbers');
      QUnit.start();
   }, 0); 
});

此测试没有语法错误,但给出了旧日期:

this test gives no syntax error but gives old date:

QUnit.test('usersInnerHTMLlength_Is24', function(assert) {
// problem: this not reads the updates done by ajax. means that are old data:
    let innerHTMLlength = $("#users").html().toString().length;
    assert.equal(innerHTMLlength, 24);
});

可能无法使用QUnit检查Ajax吗?当我在这里阅读时,我会这样: QUnit测试AJAX调用

May its not possible to check ajax with QUnit?I thougt this when i have read here:QUnit testing AJAX calls

我在Wordpress插件中使用它

I use it inside a Wordpress Plugin

推荐答案

该站点点文章的历史悠久(根据网络标准).您需要使用在文档网站中找到的较新语法:

That sitepoint article is very old (by web standards). You'll need to use the newer syntax found on the documentation website:

function someAsyncThing(value) {
  return new Promise(function (resolve, reject) {
    setTimeout(function() {
      if (value > 5) {
        resolve(value);
      } else {
        reject(new Error("bad value"));
      }
    }, 500);
  });
}


QUnit.test( "some async thing success test", function( assert ) {
  // This is what tells QUnit the test is asynchronous
  // "done" here will be a callback function used later
  var done = assert.async();
  
  // Now call your Async code, passing in a callback...
  someAsyncThing(10)
    .then(function(result) {
      // do your assertions once the async function ends...
      assert.equal(result, 10)
      
      // Now tell QUnit you're all done with the test
      done();
    })
    // we can pass the "done" callback from above into catch() to force a test failure
    .catch(done);
});

QUnit.test( "some async thing FAILURE test", function( assert ) {
  var done = assert.async();
  someAsyncThing(4)
    .then(function() {
      done(new Error("we should NOT succeed with a value < 5"));
    })
    .catch(function(err) {
      assert.equal(err.message, "bad value")
    });
});

这篇关于我试图进行QUnit异步测试以检查Ajax更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 12:21