简体   繁体   English

如何在启动Mocha测试用例之前添加延迟?

[英]How to add a delay before starting a Mocha test case?

I'm writing a unit test for my simple Node.js application using Mocha. 我正在使用Mocha为我的简单Node.js应用程序编写单元测试。 The application has a class which connects to a Mongo database, fetch the record, and store the formulated record as a field. 该应用程序有一个类,它连接到Mongo数据库,获取记录,并将制定的记录存储为字段。 Simply, the class looks like this: 简单来说,这个类看起来像这样:

SampleClass.prototype.record = []; // Store the loaded record
SampleClass.prototype.init = function(db){
    var self = this;
    self.db = mongoose.connection; // Say we already have mongoose object initialized
    self.db.once('open',function(){
        /* schema & model definitions go here */
        var DataModel = mongoose.model( /* foobar */);
        DataModel.findOne(function(err,record){
           /* error handling goes here */ 

           self.record = record; // Here we fetch & store the data
        });
    });
}

As seen from the snippet above, once the SampleClass.init() is called, the Sample.record will not instantly get populated from the database. 从上面的代码片段可以看出,一旦调用了SampleClass.init()Sample.record就不会立即从数据库中填充。 The data is asynchronously populated once the event 'open' is fired. 一旦触发事件'open',数据就会异步填充。 Thus, there will possibly be a delay after SampleClass.init() until the Sample.record is populated. 因此,在SampleClass.init()之后可能会有一个延迟,直到填充Sample.record

So it comes into a complication when I write a Mocha test like this: 因此,当我编写像这样的Mocha测试时会出现并发症:

var testSampleClass = new SampleClass();

describe('SampleClass init test',function(){
    testSampleClass.init('mydb');
    it('should have 1 record read from mydb',function(){
        assert.equal(testSampleClass.record.length,1);
    });
});

The assertion above will always fail because testSampleClass.record will not get populated straightaway after init . 上面的断言将始终失败,因为在init之后, testSampleClass.record不会立即填充。 It needs a gap of time to load the data. 加载数据需要一段时间。

How can I delay the test case so it starts a few seconds or more after testSampleClass.init is called? 我怎样才能延迟测试用例,以便在调用testSampleClass.init后启动几秒或更长时间 Is it also possible to trigger the test case right after an event of my class is fired? 在我的课程被触发后,是否也可以立即触发测试用例? Otherwise, this simple case will always fail which I know this is not correct at all. 否则,这个简单的情况总会失败,我知道这根本不正确。

Use before() or beforeEach hooks (see here and here ). 使用before()beforeEach挂钩(参见此处此处 )。 They take done callback as argument, which you must call when some asynchronous staff will be completed. 它们将done回调作为参数,您必须在完成某些异步人员时调用它。 So you test should looks like: 所以你测试应该看起来像:

describe('SampleClass init test',function(){
    before(function(done) {
        testSampleClass.init('mydb', done);
    });
    it('should have 1 record read from mydb',function(){
        assert.equal(testSampleClass.record.length,1);
    });
});

And your init method: 你的init方法:

SampleClass.prototype.record = []; // Store the loaded record
SampleClass.prototype.init = function(db, callback){
    var self = this;
    self.db = mongoose.connection; // Say we already have mongoose object initialized
    self.db.once('open',function(){
        /* schema & model definitions go here */
        var DataModel = mongoose.model( /* foobar */);
        DataModel.findOne(function(err,record){
            /* error handling goes here */

            self.record = record; // Here we fetch & store the data
            callback();
        });
    });
}

@alexpods made a great suggestion. @alexpods提出了一个很好的建议。 Add following to your test collection so that each test step will wait for 500 msec before running. 将以下内容添加到测试集合中,以便每个测试步骤在运行前等待500毫秒。

  beforeEach(function (done) {
    setTimeout(function(){
      done();
    }, 500);
  });

or in ES6 或者在ES6中

 beforeEach(done => setTimeout(done, 500));

Thanks @Timmerz for the suggestion 谢谢@Timmerz的建议

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

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