不知道为什么摩卡测试通过

Can't figure out why mocha test is passing

本文关键字:测试 摩卡 为什么 不知道      更新时间:2023-09-26

我正在学习用javascript进行测试,我正在运行这个摩卡测试

describe("Fetches Coordinates", function() {
        it("searches the database for coordinates", function() {
            var boundary = routes.setBoundries(20, 80, 20, 80)
            routes.searchCoords(boundary, function(err,data) {
                expect(data.length).to.equal(100)
            });
        });
    });

这就是它正在使用的方法

exports.searchCoords = function searchCoords(boundary, callback){
     models.sequelize.query('SELECT "data".longitude, "data".latitude, "data".ipscount FROM ('
                + ' SELECT * FROM "DataPoints" as "data"'
                + ' WHERE "data".longitude BETWEEN '
                + boundary.xlowerbound + ' and ' + boundary.xupperbound + ') data'
                + ' WHERE "data".latitude BETWEEN '
                + boundary.ylowerbound + ' and '
                + boundary.yupperbound + ';', { type: models.sequelize.QueryTypes.SELECT}).then(function(data) {
                    callback(data);
                });                 
}

当我运行测试时,似乎 Mocha 只是跳过回调并通过了。我似乎做不好。正确的语法是什么?

使用 Mocha 测试异步代码再简单不过了!只需在测试完成后调用回调即可。通过向它()添加一个回调(通常称为done),摩卡将知道它应该等待完成。

   it("searches the database for coordinates", function(done) {
        var boundary = routes.setBoundries(20, 80, 20, 80)
        routes.searchCoords(boundary, function(err,data) {
            expect(data.length).to.equal(100)
            done();
        });
    });