钩子不执行之前和之后的摩卡 [英] Mocha before and after hooks not executing

查看:58
本文介绍了钩子不执行之前和之后的摩卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的本地 cassandra 数据库上运行一个简单的测试,以检查 select 语句是否从表中返回了正确的记录数.但是,放置在 before 和 after 块之间的代码不会被调用.结果我的测试失败了.

I am trying to run a simple test on my local cassandra database to check if the select statement returns the correct no of records from the table. However, the code placed between the before and after blocks are not getting called. As a result my test simply fails.

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);
            }
        });
    })
})

推荐答案

您的测试正在执行异步操作.您需要使用回调来告诉 mocha 您的 it/before/after 功能何时完成.例如:

Your test is performing asynchronous operations. You need to use a callback to tell mocha when your it/before/after functions have finished. For example:

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();
    });
});

这篇关于钩子不执行之前和之后的摩卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆