如何编写具有异步设置和拆卸操作的测试? [英] How can I write tests that have setup and teardown operations that are asynchronous?

查看:70
本文介绍了如何编写具有异步设置和拆卸操作的测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用执行一些异步操作的库(pouchDB).为简单起见,我将其中的细节排除在外,因为我认为这是涉及异步操作的单元测试"的一个普遍问题(用引号引起来的单元测试是因为如果我正在测试集成,这并不是真正的单元测试.使用另一个库.但是,使用QUnit似乎是为其编写测试的最合适方法).

I am using a library (pouchDB) that does some async operations. To keep things simple, I will keep the details out of it as I think this is a general issue with "unit testing" involving async operations (unit testing in quotation marks because I guess this isn't truly unit testing if I am testing integration with another library. But, using QUnit seems like the most appropriate way to write tests for it).

我正在使用QUnit进行js单元测试.我有两个测试.我发现,如果我运行任何一个(同时将另一个注释掉),它们都会通过.如果我将它们同时运行,则第二次运行将失败.看来第二个程序正在运行,而第一个程序的拆卸已完成,这阻止了第二个程序的设置成功完成.

I am using QUnit to do my js unit testing. I have two tests. I am finding that if I run either one (while having the other commented out) they will pass. If I run both of them together, the run that runs second will fail. It looks like the second one is running before the teardown for the first is complete which stops the setup for the second one from completing successfully.

module("pouchDB integration specs", {

  setup: function(){

   //some setup stuff
    }

  },

  teardown: function(){

   //some teardown stuff    
    });

  }

});

asyncTest("test1", 1, function(){

test1MethodBeingTested();

document.addEventListener("completedTest1Stuff", function(){

      deepEqual(newFriendSchedule, 'new', "Test 1 Looks Good'");
      start();

    });

  });


});

asyncTest("test2", 2, function(){

  stuffBeingTestedForTest2();

  document.addEventListener("test 2 stuff done", function(){

    deepEqual(expectedTest2Result, actualTest1Result, "test 2 looks good!");
    start();    

  })

});

(不好意思缩进的道歉.所以,无论我怎么努力,SO的编辑都不喜欢我)

(Apologies for the crap indenting. SO's editor doesn't like me no matter how much I wrestle with it)

我看了马丁·福勒(Martin Fowler)对异步代码进行测试的想法,但我的收获是您应该将代码设计为不具有固有的异步性."这无济于事,因为我正在测试与不想修改的库的集成(我也不认为我必须这样做-确实已经解决了此问题?)

I had a look at Martin Fowler's take on testing asynchronous code but my takeaway from that was "you should design your code to not be inherently async." Which doesn't help, since I am testing integration with a library which I don't want to modify (nor do I think I should have to - surely this issue has been dealt with before?)

推荐答案

类似于测试方法,您可以在拆卸代码中使用 stop start 来暂停测试直到完成清理代码为止( asyncTest stop 进行隐式调用).QUnit使用计数信号量进行 stop / start 调用,因此,如果您有多个异步任务,则可以多次调用 stop .

Similar to test methods, you can use stop and start in your teardown code to pause the test runner until your clean-up code is done (asyncTest does an implicit call to stop). QUnit uses a counting semaphore for stop/start calls, so you can call stop multiple times if you've got more than one asynchronous task.

简单示例:

var teardownDone = false;
var tests = 0;

var testContent = function() {
    if (tests > 0) {
        QUnit.ok(teardownDone);
    } else {
        QUnit.ok(true);
        tests++;
    }
};

module('example', {
    teardown: function () {
        // Without this call and the subsequent start(), tests will fail.
        stop();
        setTimeout(function () {
            teardownDone = true;
            start();
        }, 1000);
    }
});

test('test1', testContent);
test('test2', testContent);

JSFiddle

这篇关于如何编写具有异步设置和拆卸操作的测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆