简体   繁体   English

如何使用设置和拆卸模块执行多个异步Qunit测试

[英]How to Do Multiple Asyncronous Qunit Tests with A Set Up and Teardown Module

I'm struggling to get some async tests set up with Qunit. 我正在努力使用Qunit设置一些异步测试。 Don't know what's going on to be honest as I'm new to testing. 我不熟悉测试,所以不知道发生了什么。 I've looked up a few similar questions on SO but none seem to work for me. 我在SO上查找了一些类似的问题,但似乎没有一个对我有用。

(function(){

  var textBox, textArea,
     fixture = document.getElementById('qunit-fixture');

  QUnit.module( "module", {

    setup: function( assert ) {

      textBox        = document.createElement('input');
      textBox.type   = 'text';
      textBox.value  = '';

      textArea       = document.createElement('textarea');
      textArea.value = ' ';

      textBox.setAttribute("id", "textBox");
      textArea.setAttribute("id", "textArea");

      fixture.appendChild(textBox);
      fixture.appendChild(textArea);

    },

    teardown: function( assert ) {

      fixture.removeChild(textBox);
      fixture.removeChild(textArea);

    }

  });

  QUnit.asyncTest( "test a", function( assert ) {

    expect(1);

    setTimeout(function() {
      textBox.value = 'a'
    }, 250);

    setTimeout(function() {
      assert.deepEqual(textBox.value, 'a');
      QUnit.start();
    }, 500);

  });

  QUnit.asyncTest( "test b", function( assert ) {

   expect(1);

    setTimeout(function() {
      textBox.value = 'b'
    }, 250);

    setTimeout(function() {
      assert.deepEqual(textBox.value, 'b');
      QUnit.start();
    }, 500);

  });

}());

When I run this in the browser it says: 1. module: test b(1) , so I guess it's only the running the last test or the first hasn't finished or something? 当我在浏览器中运行它时,它说: 1. module: test b(1) ,所以我猜它只是在运行最后一个测试或第一个尚未完成,还是其他? Confused, please help. 感到困惑,请帮忙。

Hmm... I've copied your code into a test file and run it and everything works fine, I see both tests and both pass. 嗯...我已经将您的代码复制到测试文件中并运行它,并且一切正常,我看到了两个测试都通过了。 That said, QUnit does allow you to rerun individual tests... can you check the URL in your browser? 就是说,QUnit确实允许您重新运行单个测试...可以在浏览器中检查URL吗? Does it indicate a test number to run? 它是否指示要运行的测试编号? Are there any errors in the console? 控制台中是否有任何错误?

Additionally, note that the #qunit-fixture div is automatically reset after each test in that module, so the teardown mehtod you have should not be necessary. 另外,请注意,在该模块中的每次测试后, #qunit-fixture div都会自动重置,因此您不需要teardown方法。 Instead, you can simply add an input and textarea to that #qunit-fixture div in the HTML file and then let QUnit reset it for you. 相反,您可以简单地在HTML文件中的#qunit-fixture div中添加输入和文本区域,然后让QUnit为您重置它。

My test.html file: 我的test.html文件:

<!doctype html>
<html>
  <head>
    <link rel="stylesheet" href="vendor/qunit-1.14.0.css" />
  </head>
  <body>
    <div id="qunit"></div>
    <div id="qunit-fixture"></div>

    <script src="vendor/qunit-1.14.0.js"></script>
    <script>
      // your code here...
    </script>
  </body>
</html>

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

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