{"action":"created","issue":{"url":"https://api.github.com/repos/thlorenz/proxyquire/issues/141","repository_url":"https://api.github.com/repos/thlorenz/proxyquire","labels_url":"https://api.github.com/repos/thlorenz/proxyquire/issues/141/labels{/name}","comments_url":"https://api.github.com/repos/thlorenz/proxyquire/issues/141/comments","events_url":"https://api.github.com/repos/thlorenz/proxyquire/issues/141/events","html_url":"https://github.com/thlorenz/proxyquire/issues/141","id":186822774,"number":141,"title":"Failing mocking node-pg object method","user":{"login":"benoittgt","id":8417720,"avatar_url":"https://avatars.githubusercontent.com/u/8417720?v=3","gravatar_id":"","url":"https://api.github.com/users/benoittgt","html_url":"https://github.com/benoittgt","followers_url":"https://api.github.com/users/benoittgt/followers","following_url":"https://api.github.com/users/benoittgt/following{/other_user}","gists_url":"https://api.github.com/users/benoittgt/gists{/gist_id}","starred_url":"https://api.github.com/users/benoittgt/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/benoittgt/subscriptions","organizations_url":"https://api.github.com/users/benoittgt/orgs","repos_url":"https://api.github.com/users/benoittgt/repos","events_url":"https://api.github.com/users/benoittgt/events{/privacy}","received_events_url":"https://api.github.com/users/benoittgt/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2016-11-02T14:51:52Z","updated_at":"2016-11-07T13:16:35Z","closed_at":"2016-11-02T14:56:11Z","body":"Hello\r\n\r\nI know it's not related to proxyquire directly but I'm stuck since 3 days on this issue : \r\n\r\nI'm building a AWS lambda that connect to a postgres database. I wanted to mock this database for testing. \r\nIn my code_base I have something similar to : \r\n```javascript\r\nvar pg = require('pg');\r\nvar client = new pg.Client();\r\n\r\nfunction query_aggregate(callback) {\r\n  client.connect(function (err) {\r\n    if (err) throw err;\r\n    client.query('SELECT $1::text as name', ['brianc'], function (err, result) {\r\n      if (err) callback(err);\r\n      callback(null, result.rows)\r\n      client.end(function (err) {\r\n        if (err) callback(err);\r\n      });\r\n    });\r\n  });\r\n}\r\n```\r\n\r\nI have this simple mocha test : \r\n```javascript\r\n  describe('query_aggregate', function() {\r\n    it('fail to connect to postgres', function(done){\r\n      proxyquire('../index', {\r\n        Cient: function(host) {\r\n            console.log(host); // print pg://host:3456\r\n            this.connect = function(callback) {\r\n              console.log('Here!'); // never printed\r\n              callback(new Error('Failed to connect to postgres'))\r\n            }\r\n          }\r\n        }\r\n      });\r\n      testingAggregate.query_aggregate(function(err, data){\r\n        expect(err).to.equal('Failed to connect to postgres');\r\n        done();\r\n      });\r\n    });\r\n  });\r\n```\r\n```\r\n  1) Testing aggregate function query_aggregate fail to connect to postgres:\r\n     Uncaught AssertionError: expected [Error: connect EHOSTDOWN 168.410.131.63:5439 - Local (0.0.0.0:0)] to equal 'Failed to connect to postgres'\r\n```\r\n\r\nDo you have any idea why I can't mock `connect`?\r\n\r\nThanks in advance"},"comment":{"url":"https://api.github.com/repos/thlorenz/proxyquire/issues/comments/258832563","html_url":"https://github.com/thlorenz/proxyquire/issues/141#issuecomment-258832563","issue_url":"https://api.github.com/repos/thlorenz/proxyquire/issues/141","id":258832563,"user":{"login":"benoittgt","id":8417720,"avatar_url":"https://avatars.githubusercontent.com/u/8417720?v=3","gravatar_id":"","url":"https://api.github.com/users/benoittgt","html_url":"https://github.com/benoittgt","followers_url":"https://api.github.com/users/benoittgt/followers","following_url":"https://api.github.com/users/benoittgt/following{/other_user}","gists_url":"https://api.github.com/users/benoittgt/gists{/gist_id}","starred_url":"https://api.github.com/users/benoittgt/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/benoittgt/subscriptions","organizations_url":"https://api.github.com/users/benoittgt/orgs","repos_url":"https://api.github.com/users/benoittgt/repos","events_url":"https://api.github.com/users/benoittgt/events{/privacy}","received_events_url":"https://api.github.com/users/benoittgt/received_events","type":"User","site_admin":false},"created_at":"2016-11-07T13:16:35Z","updated_at":"2016-11-07T13:16:35Z","body":"I finally did : \r\n\r\n```javascript\r\n  describe('queryAggregate', function() {\r\n    let pgStub = { };\r\n    let redhsiftAggregate = proxyquire('../../lib/redshift_aggregate', { 'pg': pgStub });\r\n\r\n    it('fail to connect to postgres', function(done){\r\n      pgStub.Client = function() {\r\n        this.connect = function(callback) {\r\n          callback(new Error('Failed to connect to postgres'));\r\n        };\r\n      };\r\n      redhsiftAggregate.queryAggregate(query, function(err, data){\r\n        expect(err).to.equal('Failed to connect to postgres');\r\n        done();\r\n      });\r\n    });\r\n  });"}}
{"actor":{"display_login":"benoittgt"}}