模拟来自发射器的 'end' 信号,但它不会被调用

Mock 'end' signal from emitter, but it doesn't get called

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

为什么发射器不发射'end'?

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(,,,,,,,,,,,,,,,,,,)';
        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)

如果它真的发出了'end'信号。然后它应该说 'Insert record completed'.

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

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