2

我正在使用 Mocha 并且应该用于测试简单的数据库查询,我正在尝试为简单的 Moongose 模式函数运行异步测试,但每次都会遇到超时超出错误。

  Error: timeout of 15000ms exceeded
  at null.<anonymous> (/usr/local/lib/node_modules/mocha/lib/runnable.js:165:14)
  at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)

我什至使用了 this.timeout(15000)并且还在 mocha 命令中尝试了--timeout 15000但没有成功,我给出的超时时间是多少,我收到了这个错误。只有同步测试通过。以下是我要测试的测试和功能。

我的摩卡测试:-

   describe('#getFacility()', function () {
    this.timeout(15000);
    it('should get facility successfully', function (done) {
        db.getFacilites(Data.testFacility, function (err, facilities) {
            if (err) throw err;
            facilities.Name.should.equal(Data.testFacility.body.Name);
            done();
        })
    });
});

我的数据:-

testFacility : {
    params:  { clientId:"51c85c3267b6fc1553000001" }
},

我的获取函数

getFacilites: function (req, res) {
    Facility.find({Client: req.params.clientId}, function (err, facilities) {
        if(err) {
            res.send(500,err);
        } else if (!facilities) {
            res.send(404, "facilities not found");

        } else if (req.query.format && req.query.format === 'select') {
            var result = facilities.map (function (f) {
                return { value: f._id.toString(), text: f.Name }
            });
            res.json(result);

        } else {
            console.log("Cannot Retrieve Facilities");
        }
    });
}

我什至还为查询创建了一个新的单独函数,但它仍然无法正常工作。功能看起来像这样的任何想法。

describe('#getFacility() direct from DB', function () {
    it('should get facility successfully from db', function (done) {
        Client_data.Facility.find({Client: Data.testFacility.params.clientId}, function(err, facilities) {
            if (err) throw (err);
            if (facilities) {
                facilities.forEach(function (f) {
                    console.log(f);
                });
                done();
            }
        });
    });
});

如果我尝试在查询后调用 done() 回调,则测试通过,但这对我来说也不好看。

describe('#addFacility()', function () {
    it('should add facility successfully', function (done) {
        API_Calls.addFacility(Data.testFacility, function (doc) {
            doc.Name.should.equal(Data.testFacility.body.Name);
        });
        done();
    });
});
4

1 回答 1

2

Your getFacilities is taking a req, res and next and you're passing it something totally different in your test (a testFacility object and a callback).

I think your getFacilities method definition should not be taking req, res and next, maybe a clientId and next only and then depending on the callback of next you can create the appropriate response.

于 2013-07-12T21:58:58.990 回答