{"action":"created","issue":{"url":"https://api.github.com/repos/thlorenz/proxyquire/issues/63","labels_url":"https://api.github.com/repos/thlorenz/proxyquire/issues/63/labels{/name}","comments_url":"https://api.github.com/repos/thlorenz/proxyquire/issues/63/comments","events_url":"https://api.github.com/repos/thlorenz/proxyquire/issues/63/events","html_url":"https://github.com/thlorenz/proxyquire/issues/63","id":76825118,"number":63,"title":"Possible to stub constructors?","user":{"login":"alexjamesbrown","id":437743,"avatar_url":"https://avatars.githubusercontent.com/u/437743?v=3","gravatar_id":"","url":"https://api.github.com/users/alexjamesbrown","html_url":"https://github.com/alexjamesbrown","followers_url":"https://api.github.com/users/alexjamesbrown/followers","following_url":"https://api.github.com/users/alexjamesbrown/following{/other_user}","gists_url":"https://api.github.com/users/alexjamesbrown/gists{/gist_id}","starred_url":"https://api.github.com/users/alexjamesbrown/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alexjamesbrown/subscriptions","organizations_url":"https://api.github.com/users/alexjamesbrown/orgs","repos_url":"https://api.github.com/users/alexjamesbrown/repos","events_url":"https://api.github.com/users/alexjamesbrown/events{/privacy}","received_events_url":"https://api.github.com/users/alexjamesbrown/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2015-05-15T19:12:51Z","updated_at":"2015-05-15T19:28:27Z","closed_at":"2015-05-15T19:21:30Z","body":"For example-\r\n\r\nIn my 'email.js' I have:\r\n\r\n    var mandrill = require('mandrill-api/mandrill');\r\n    var mandrill_client = new mandrill.Mandrill('API_KEY_HERE');\r\n\r\n    exports.sendTemplate = function(templateName, to, variables, callback) {\r\n\r\n        console.log('mandrill_client === ', mandrill_client)\r\n        \r\n        var message = {\r\n            //omitted for brevity\r\n        };\r\n\r\n        mandrill_client.messages.sendTemplate({\r\n            template_name: templateName,\r\n            template_content: [],\r\n            message: message,\r\n        }, function(result) {\r\n            //... do something\r\n            return callback();\r\n        }, function(err) {\r\n            //...\r\n        });\r\n    }\r\n\r\n\r\nThen, in my test, I want to stub out mandrill, so that when a new mandrill_client is created, it gets my stubbed version, ultimately with a sinon.spy on mandrill_client.messages.sendTemplate\r\n\r\n    //not sure what mandrillStub should be??\r\n\r\n    email = proxyquire('../../../app/helpers/email', {\r\n        'mandrill-api/mandrill': mandrillStub\r\n    });\r\n\r\nIs this possible?\r\n"},"comment":{"url":"https://api.github.com/repos/thlorenz/proxyquire/issues/comments/102500745","html_url":"https://github.com/thlorenz/proxyquire/issues/63#issuecomment-102500745","issue_url":"https://api.github.com/repos/thlorenz/proxyquire/issues/63","id":102500745,"user":{"login":"bendrucker","id":808808,"avatar_url":"https://avatars.githubusercontent.com/u/808808?v=3","gravatar_id":"","url":"https://api.github.com/users/bendrucker","html_url":"https://github.com/bendrucker","followers_url":"https://api.github.com/users/bendrucker/followers","following_url":"https://api.github.com/users/bendrucker/following{/other_user}","gists_url":"https://api.github.com/users/bendrucker/gists{/gist_id}","starred_url":"https://api.github.com/users/bendrucker/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bendrucker/subscriptions","organizations_url":"https://api.github.com/users/bendrucker/orgs","repos_url":"https://api.github.com/users/bendrucker/repos","events_url":"https://api.github.com/users/bendrucker/events{/privacy}","received_events_url":"https://api.github.com/users/bendrucker/received_events","type":"User","site_admin":false},"created_at":"2015-05-15T19:28:27Z","updated_at":"2015-05-15T19:28:27Z","body":"You're quite welcome! What I mean by the above comments is that often times you may be able to do something like this:\r\n\r\n```js\r\n// ./source.js\r\nvar foo = require('foo')\r\nreturn {\r\n  simple: function (baz) {\r\n    return foo.bar(baz)\r\n  },\r\n  hard: function () {\r\n    return foo()\r\n  }\r\n}\r\n```\r\n\r\n```js\r\n// ./test.js\r\nvar foo = require('foo')\r\nsinon.spy(foo, 'bar')\r\n```\r\n\r\nFor testing `simple` don't actually need proxyquire, though explicitly providing module stubs might be a good idea. Whereas if you needed to change `foo` itself (i.e. for `hard`), you can't accomplish that without proxyquire."}}