从发射器模拟“结束”信号,但不会被调用

时间:2015-07-04 14:35:47

标签: javascript unit-testing mocha pg proxyquire

我正在使用mocha + unit.js + proxyquire进行单元测试。现在我被困在嘲笑的发射器。第一个被模拟的发射器,它在'行'信号上是假的,完成了。第二个我跟第一个一样。由于这不是一个很大的区别,但我在这里有一个问题。

为什么发射器不发射'结束'?

userHandler_post.js:

var pg = require('pg');
var connectionString = require('./postgres.ini');
var pgdown = require('./pgdown.js');

exports.post = function(req,res, callback){
    var cb_is_func = (typeof(callback)==='function');
    // Grab data from http request
    var adata = [req.body.username, 
         req.body.password,
         req.body.privilege,
         req.body.firstname,    
         req.body.middlename,   
         req.body.lastname,
         req.body.streetaddress, //rename
         req.body.subdistrict,      
         req.body.district,     
         req.body.province,
         req.body.country,          
         req.body.zipcode,
         req.body.email,
         req.body.phonecountrycode,
         req.body.phoneareacode,
         req.body.phonenumber, //rename
         req.body.faxcountrycode,
         req.body.faxareacode,
         req.body.faxnumber]; //rename

    // Get a Postgres client from the connection pool
    pg.connect(connectionString, function(err, client, done) {

    // Handle Errors
    if(err) {
        done();
        pgdown.pgdown(res);
        if(cb_is_func){callback();}
    }else{

        // SQL Query > Insert Data
        var func_ = 'SELECT Dugong.Users_Add($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19)';
        var addUser_ = client.query(func_, adata);

        addUser_.on('error', function(error){
        var data = {success : false, 
                username : req.body.username,
                reason : {errmsg : 'Unable to add user',
                      errid : 'addUser_' }};
        done();
        res.json(data);
        if(cb_is_func){callback(data);}
        });

        addUser_.on('end',function(result){
        var data = {success : true, username : req.body.username};
        done();
        res.json(data);
        if(cb_is_func){callback(data);}
        });
    }
    });
    pg.end();
};

userHandler_post.test.js:

var httpMocks = require('node-mocks-http');
var test = require('unit.js');

var real_userHandler = require('../routes/userHandler_post.js');

var proxyquire =  require('proxyquire'),
        pgStub   =  { };

var events = require('events');


describe('userHandler_post : ', function () {

    var request;
    beforeEach(function (){
            request  = httpMocks.createRequest({
          method: 'POST',
          url: '/users',
          body : { Username : 'Yoda', 
                                ... //Omit to save question lines.
                                FaxNum: '',
                            }
    });
    });

    it('should show connetion error. If cannot connect to database.', function (done) {

        pgStub.connect = function (aconnectionString, cb){
            cb('Connection refused',null,null); 
        };

    var response = httpMocks.createResponse();

    var userHandler_post = proxyquire('../routes/userHandler_post.js', { 'pg' : pgStub} );

    userHandler_post.post(request,response);    //Let it update response
        var answer = response._getData();
        test.value(answer).match('Connection refused');

        done(); //Need
    });

    it('should show query error. If database rejects query.', function (done) {

        pgStub.connect = function (aconnectionString, cb){
            var client = {};
            client.query = function(querystr){
                var emitter = new events.EventEmitter();

                setTimeout(function() {
                    emitter.emit('error', {detail: 'Query error'})
                }, 0);      

                return emitter; 
            };

            var done = function(){};    
            cb(null,client,done);

        };

        var response = httpMocks.createResponse();

    var userHandler_post = proxyquire('../routes/userHandler_post.js', { 'pg' : pgStub} );

    userHandler_post.post(request, response, function(){
            var answer = response._getData();
            test.value(answer).match('Query error');
    });

        done(); //Need
    });

    it('should show several records. If database processes query.', function (done) {

        pgStub.connect = function (aconnectionString, cb){
            var client = {};
            client.query = function(querystr){

                var emitter = new events.EventEmitter();
                console.log('below emitter');

                setTimeout(function() {
                    console.log('-*************--');
                    emitter.emit('end', {a : 1});   
                }, 0);      //

                return emitter; 
            };

            var done = function(){};
            cb(null, client, done);

        };

        var response = httpMocks.createResponse();

    var userHandler_post = proxyquire('../routes/userHandler_post.js', { 'pg' : pgStub} );

        userHandler_post.post(request, response, function(){
            var answer = response._getData();
            console.log(answer);
//          test.value(answer).match('Connection refused');
    });

//      console.log(response._getData());
        done(); //Need
    });

});

终端:

mocha testing/userHandler_post.test.js


  userHandler_post : 
    ✓ should show connetion error. If cannot connect to database.
    ✓ should show query error. If database rejects query.
below emitter
    ✓ should show several records. If database processes query.


  3 passing (10ms)

如果真的发出'结束'信号。然后它应该说'插入记录已完成'。

1 个答案:

答案 0 :(得分:0)

我找到了。
升级节点从v0.10.29升级到v0.12.6

npm cache clean -f
npm install -g n
aptitude install curl
n stable