【问题标题】:Qunit beforeEach, afterEach - asyncQunit beforeEach, afterEach - 异步
【发布时间】:2014-12-31 07:32:03
【问题描述】:

由于 start()、stop() 将在 Qunit 2.0 中被删除,通过 beforeEach、afterEach 方法进行异步设置和拆卸的替代方案是什么?例如,如果我希望 beforeEach 等待承诺完成?

【问题讨论】:

    标签: testing asynchronous qunit


    【解决方案1】:

    QUnit 基本上希望人们停止使用global methods(不仅仅是start()stop(),还有test()expect() 等)。因此,从版本 1.16.0 开始,您应该始终使用全局命名空间 (QUnit) 或传递给 test() 函数的 assert API 参数。这包括新的async control

    QUnit.test( "testing async action", function( assert ) {  // <-- note the `assert` argument here
        var done = assert.async();  // tell QUnit we're doing async actions and
                                    // hold onto the function it returns for later
    
        setTimeout(function() {  // do some async stuff
            assert.ok( true, "This happened 100 ms later!" );
    
            done();  // using the function returned from `assert.async()` we 
                     // tell QUnit we're don with async actions
        }, 100);
    });
    

    如果您熟悉旧的 start()stop() 的做事方式,您应该会发现这非常相似,但更加分隔和可扩展。

    因为async()方法调用是在assert参数进入测试,所以不能在beforeEach()函数中使用。如果你有一个例子说明你之前是如何做到的,请发布它,我们可以尝试找出如何将它融入新的方式。

    更新

    我之前的错误是,assert 对象被传递到模块上的 beforeEachafterEach 回调中,因此您应该能够执行与测试相同的逻辑:

    QUnit.module('set of tests', {
        beforeEach: function(assert) {
            var done = assert.async();
            doSomethingAsync(function() {
                done(); // tell QUnit you're good to go.
            });
        }
    });
    

    (在 QUnit 1.17.1 中测试)

    【讨论】:

    • 我也有同样的问题。这是我对 Ember 应用程序进行集成测试时的用例:gist.github.com/tygriffin/0d8cae37c47b243588b9
    • @niftygrifty 我建议在任何测试或模块运行之前调用一次,并使用QUnit.config.autostart = false; 延迟QUnit 的启动,然后在$.get() 完成保存返回数据并调用QUnit.start();让事情顺利进行。
    • 这对于我链接到的孤立用例来说是个好主意,但理想情况下,我希望有一个拆卸端点以及许多不同的设置端点。如果不是在每个测试之间,我还想至少在每个模块之间进行设置和拆卸。据我所知,QUnit.start() 只能被调用一次来启动整个测试套件。对吗?
    • 所以实际上......现在看起来assert 对象正在被传递到 beforeEach/afterEach 方法中!我会更新我的答案,但看起来你可以使用与模块设置和拆卸测试中相同的语法。
    • 非常感谢您仔细检查!我希望我拥有这个问题,以便我可以将其标记为已接受。
    【解决方案2】:

    看到没有人回答 beforeEach/afterEach 部分:测试套件应该在页面加载后立即运行。如果不能立即实现,则求助于配置 QUnit:

    QUnit.config.autostart = false;
    

    并继续设置您的测试套件(初始化测试,将它们提供给 QUnit,异步等待某些组件加载,无论是 AJAX 还是其他任何东西),您的站点,最后,当它准备好时,运行:

    QUnit.start();
    

    QUnit's docsite 已覆盖。

    【讨论】:

      【解决方案3】:

      Ember Qunit,曾经存在过beforeEach/setupafterEach/teardown并存了一段时间。

      见公关:https://github.com/emberjs/ember-qunit/pull/125

      【讨论】:

        猜你喜欢
        • 2017-02-27
        • 1970-01-01
        • 2019-01-03
        • 1970-01-01
        • 2013-06-23
        • 1970-01-01
        • 1970-01-01
        • 2019-12-04
        • 1970-01-01
        相关资源
        最近更新 更多