2016-10-31 35 views
1

我有類似模擬的Postgres調用節點

const pg = require('pg'); 
const async = require('async'); 
const conn = 'pg://username:[email protected]_db:2435/db'; 
const client = new pg.Client(conn); 

exports.handler = function(event, context) { 
    async.waterfall([ 
    query_aggregate(callback), 
    my_next_function(rows, callback) 
    ], 
    function (err) { 
     if (err) { 
     context.fail(err); 
     } else { 
     context.succeed('Succeed'); 
     } 
    }); 
}; 

function query_aggregate(callback) { 
    client.connect(function (err) { 
    if(err) callback(err); 

    const query = 'SELECT shop_id, COUNT(DISTINCT(user_id)) from connections GROUP BY sshop_id'; 
    client.query(query, function (err, result) { 
     if (err) callback(err); 

     client.end(function (err) { 
     if (err) callback('Error in query:', err); 
     callback(null, result.rows); 
     }); 
    }); 
    }); 
} 

if (typeof exports !== 'undefined') { 
    exports.query_aggregate = query_aggregate; 
} 

一個代碼,但在測試中,我想驗證什麼是發生在我有錯誤,也是我正確地返回一個回調與rows。但如何模擬client.connectclient.query

目前我只是嘗試從https://stackoverflow.com/a/10124424/2747638答案與sinon

const testingAggregate = require('../index.js'); 
const assert = require('assert'); 
const expect = require('chai').expect; 
const sinon = require('sinon'); 

describe('Testing aggregate function', function() { 
    describe('query_aggregate', function() { 
    it('fail to connect to postgres', function(done){ 
    let mock; 
    mock = sinon.mock(require('pg')); 
    mock.expect('connect').once(); 
     testingAggregate.query_aggregate(function(err, data){ 
     expect(err).to.equal('Failed to connect to postgres'); 
     done(); 
     }); 
    mock.verify(); 
    }); 
    }); 
}); 

我看到一個github issuepg-pool但沒有任何具體的例子。

編輯1:

我有這個簡單的摩卡測試:使用proxyquire但它失敗。

describe('query_aggregate', function() { 
    it('fail to connect to postgres', function(done){ 
     proxyquire('../index', { 
     Cient: function(host) { 
      console.log(host); // print pg://host:3456 
      this.connect = function(callback) { 
       console.log('Here!'); // never printed 
       callback(new Error('Failed to connect to postgres')) 
      } 
      } 
     } 
     }); 
     testingAggregate.query_aggregate(function(err, data){ 
     expect(err).to.equal('Failed to connect to postgres'); 
     done(); 
     }); 
    }); 
    }); 

測試結果:

1) Testing aggregate function query_aggregate fail to connect to postgres: 
    Uncaught AssertionError: expected [Error: connect EHOSTDOWN 168.410.131.63:3456 - Local (0.0.0.0:0)] to equal 'Failed to connect to postgres' 

你有任何想法,爲什麼我不能嘲笑connect

在此先感謝您的幫助。

回答

1

我終於使用了proxyquire。這個答案可能不是最好的。

要測試功能query_aggregate我所做的:

const proxyquire = require('proxyquire').noCallThru(); 
const assert = require('assert'); 
const expect = require('chai').expect; 

describe('Testing aggregate function', function() { 
    describe('query_aggregate', function() { 
    it('fail to connect to postgres', function(done){ 
     let pgStub = { 
     Client: function(host) { 
      this.connect = function(callback) { 
      callback(new Error('Failed to connect to postgres')); 
      }; 
     } 
     }; 
     let testingAggregate = proxyquire('../index', { 'pg': pgStub }); 

     testingAggregate.query_aggregate(function(err, data){ 
     expect(err).to.deep.equal(new Error('Error: Failed to connect to postgres')); 
     done(); 
     }); 
    }); 


    it('fail on query and return an error', function(done){ 
     let pgStub = { 
     Client: function(host) { 
      this.connect = function(callback) { callback(); }; 
      this.query = function(query, callback) { 
      callback(new Error('Failed to query postgres')); 
      }; 
     } 
     }; 
     let testingAggregate = proxyquire('../index', { 'pg': pgStub }); 

     testingAggregate.query_aggregate(function(err, data){ 
     expect(err).to.deep.equal(new Error('Error: Failed to connect to postgres')); 
     done(); 
     }); 
    }); 

    it('succeed on query and return rows', function(done){ 
     let resultRows = [{ a:1 },{ b:2 }]; 
     let pgData = { rows: resultRows }; 
     let pgStub = { 
     Client: function(host) { 
      this.connect = function(callback) { callback(); }; 
      this.query = function(query, callback) { 
      expect(query).to.eq('SELECT shop_id, COUNT(DISTINCT(user_id)) from connections GROUP BY shop_id'); 
      callback(null, pgData); 
      }; 
     } 
     }; 
     let testingAggregate = proxyquire('../index', { 'pg': pgStub }); 

     testingAggregate.query_aggregate(function(err, data){ 
     expect(data).to.deep.equal(resultRows); 
     done(); 
     }); 
    }); 
    }); 
});