Promise中的单元测试pg发射器得到了

时间:2015-10-19 04:22:09

标签: javascript node.js unit-testing

我正在创建Javascript的单元测试。这是我的代码。 我在quizHandler_getfull.js中有两个函数。
1. rreject:它将以相同的格式处理错误 2. getquizid:它会查询然后发出信号
下一个文件是单元测试文件。我使用带有proxyquire的mocha框架。 我创建了客户端对象然后放入目标函数 然后,我在参考时提到了错误。

我怀疑单位测试人员从未调用过client.query() 我在哪里错过了?

quizHandler_getfull.js

exports.rreject = function(error,pid){
    //pid = product id
    var data = { success : false,
                 id : pid ,
                 reason : { errmsg : error.detail,
                            errid : error.code}};
    console.log('error');
    return data;
};

exports.getquizid = function(client,pid){
    return new Promise(function(fulfill,reject){
    var qstr = "SELECT quiz_id, to_char(maxtime, 'HH24:MI:SS') as maxtime, subject_id, author_id, description, difficulty, name, product_id, to_char(lastmodified at time zone 'utc', 'YYYY-MM-DD\
\"T\"HH24:MI:SSZ') as lastmodified FROM dugong.quiz WHERE product_id = $1";
    var qzid = client.query(qstr,[pid]);
    qzid.on('error',function(err){
        console.log('error');
        console.log('getquizid function');
        console.log(err);
        var data = exports.rreject(err,pid);
        reject(data);
    });
    qzid.on('row',function(row){
        fulfill(row);
    });
    qzid.on('end',function(row){
        if(row.rowCount == 0 ){
        var err = { detail : 'ID not found',
                code : 'Quiz_101'};
        reject(err);
        }
    });
    });    
};

quizHandler_getfull.test.js

var httpMocks = require('node-mocks-http');
var test = require('unit.js');
var assert = require('assert');
var real_quizHandler = require('../../routes/quizHandler_getfull.js');

var proxyquire =  require('proxyquire')
, pgStub   =  { };
var quizHandler_getfull, response, request
describe('quizHandler_getfull : ', function () {

    beforeEach(function(){
    request  = httpMocks.createRequest({
        method: 'GET',
        url: '/quizzes/full',
        params : {id : 1}
    });
    pgStub.end = function(){}; 
    quizHandler_getfull = proxyquire('../../routes/quizHandler_getfull.js', { 'pg' : pgStub} );
    response = httpMocks.createResponse();
    });
    it('getquizid should show query error. If database rejects query.', function (done) {
    var client = {};
    client.query = function(querystr, pid){
        var emitter = new events.EventEmitter();
        setTimeout(function(){
        emitter.emit('error', {detail: 'This is fake query error', code : 'error666'})
        },0);
        console.log('hi there');
        return emitter;
    };

    //Memo : put anonymous done() to avoid conflict with mocha's done.
    Promise.all([quizHandler_getfull.getquizid(client,1)]).then(
        //function fulfill(output){},
        null,
        function reject(er){
        console.log('before done');
        console.log(er);
        console.log('done here');
        done();
        }
    );
    });
});

我的错误

before done
[ReferenceError: events is not defined]
done here

1 个答案:

答案 0 :(得分:1)

好像你没有导入 事件 。 根据node.js文档,它必须是:

 var EventEmitter = require('events');

https://nodejs.org/api/events.html