sequelize函数不会在Mocha中引发异常

时间:2017-02-22 15:36:30

标签: mocha sequelize.js

我对mocha框架有些新意。此代码应该抛出异常,但不是。 (为简单起见,将所有代码放在测试中)

describe("Test", function() {
    it("this should throw an error", function() {
        var Sequelize = require("sequelize");
        var sequelize = new Sequelize({
            dialect: 'sqlite',
            storage: 'test.sqlite3'
        });
        var foo = sequelize.define('Foo', {
            var_a: Sequelize.INTEGER,
            var_b: Sequelize.STRING
        });
        sequelize.sync().then(function(err) {
            throw('should throw an error here); // !!! Should throw an exception here
        });
    });
});

有趣的是,当我在node.js中直接运行代码时(当然没有"描述"""行),它会引发一个类似的异常。我对摩卡有点新鲜,是否有一些非常简单的东西我不知道?

1 个答案:

答案 0 :(得分:0)

找到解决方案。 From(https://mochajs.org/#getting-started):“通过向它添加回调(通常名为”done“),Mocha将知道它应该等待调用此函数来完成测试。”

describe("Test", function() {
    it("this should throw an error", function(done) {
        var Sequelize = require("sequelize");
        var sequelize = new Sequelize({
            dialect: 'sqlite',
            storage: 'db/test_db.sqlite3'
        });
        var foo = sequelize.define('Foo', {
            var_a: Sequelize.INTEGER,
            var_b: Sequelize.STRING
        });
        sequelize.sync().then(function(err) {
            done();
            throw('got here');
        });
    });
});