摩卡前后挂钩不执行

Mocha before and after hooks not executing

我正在尝试 运行 在我的本地 cassandra 数据库上进行一个简单测试,以检查 select 语句 returns 中 table 的记录数是否正确。但是,放置在 before 和 after 块之间的代码没有被调用。结果我的测试完全失败了。

var assert = require('assert');
var cassandra = require('cassandra-driver');
var async = require('async');

//Connect to the cassandra cluster, assuming that the keyspace & columnspace already exists
var client = new cassandra.Client({contactPoints: ['127.0.0.1'], keyspace: 'demo'}); 
describe('Cassandra is up and running', function() {
    before(function() {
        //Check if the connection got established successfuly 
        client.connect(function(err) {
            if(err){
                console.log("Oops, something went wrong : " + err);
            }
            console.log('I al here 1');
        });

        //Insert a few data to column space
        var queries = [
        {
        query: 'INSERT INTO users(key, name, emails) VALUES (?, ?, ?)',
        params: ['mick-jagger1', 'Sir Mick Jagger 1', 'mick1@company.com']
        },
        {
        query: 'INSERT INTO users(key, name, emails) VALUES (?, ?, ?)',
        params: ['mick-jagger2', 'Sir Mick Jagger 2', 'mick2@company.com']
        },
        {
        query: 'INSERT INTO users(key, name, emails) VALUES (?, ?, ?)',
        params: ['mick-jagger3', 'Sir Mick Jagger 3', 'mick3@company.com']
        },
        {
        query: 'INSERT INTO users(key, name, emails) VALUES (?, ?, ?)',
        params: ['mick-jagger4', 'Sir Mick Jagger 4', 'mick4@company.com']
        }
        ];

        client.batch(queries, { prepare: true }, function(err) {
            if(err){
                console.log("Oops, something went wrong : " + err);
            }
            console.log('Sample data inserted into column space');
        });
    })

    it('should return 4 when four rows of data are inserted into demo table', function() {
        var count = 0;
        client.execute('SELECT COUNT(*) FROM users', function(err, result) {
            count = result.rows[0];
        });
        assert.equal(4, count);
        console.log("I am here : " + count);
    })

    after(function() {
        client.execute('TRUNCATE users', function(err, result) {
            if(err){
                console.log("Oops, something went wrong : " + err);
            }
        });
    })
})

您的测试正在执行异步操作。当 it/before/after 函数完成时,您需要使用回调来通知 mocha。例如:

before(function(done) {
    // ...
    client.batch(queries, {
        prepare: true
    }, function(err) {
            if (err) {
                console.log("Oops, something went wrong : " + err);
            }
            console.log('Sample data inserted into column space');
            done();
        });
});

和:

it('should return 4 when four rows of data are inserted into demo table', function(done) {
    var count = 0;
    client.execute('SELECT COUNT(*) FROM users', function(err, result) {
        count = result.rows[0];
        assert.equal(4, count);
        console.log("I am here : " + count);
        done();
    });
});

ES6更新

对于这种情况,您现在可以使用 async/await 模式:

before(async () => {
        // ...
        try {
         await client.batch(queries, {
            prepare: true
          });
        } catch (err) {
        if(err) {
         console.log("Oops, something went wrong : " + err)
          }
        console.log('Sample data inserted into column space');
        }
    });

it('should return 4 when four rows of data are inserted into demo table', async () => {
      var count = 0;
      await client.execute('SELECT COUNT(*) FROM users', (err, result) => { 
       count = result.rows[0];
        assert.equal(4, count);
        console.log("I am here : " + count);
      });
    });