{"action":"created","issue":{"url":"https://api.github.com/repos/hapijs/discuss/issues/316","repository_url":"https://api.github.com/repos/hapijs/discuss","labels_url":"https://api.github.com/repos/hapijs/discuss/issues/316/labels{/name}","comments_url":"https://api.github.com/repos/hapijs/discuss/issues/316/comments","events_url":"https://api.github.com/repos/hapijs/discuss/issues/316/events","html_url":"https://github.com/hapijs/discuss/issues/316","id":166395412,"number":316,"title":"stubbing best practices","user":{"login":"jamesdixon","id":110114,"avatar_url":"https://avatars.githubusercontent.com/u/110114?v=3","gravatar_id":"","url":"https://api.github.com/users/jamesdixon","html_url":"https://github.com/jamesdixon","followers_url":"https://api.github.com/users/jamesdixon/followers","following_url":"https://api.github.com/users/jamesdixon/following{/other_user}","gists_url":"https://api.github.com/users/jamesdixon/gists{/gist_id}","starred_url":"https://api.github.com/users/jamesdixon/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jamesdixon/subscriptions","organizations_url":"https://api.github.com/users/jamesdixon/orgs","repos_url":"https://api.github.com/users/jamesdixon/repos","events_url":"https://api.github.com/users/jamesdixon/events{/privacy}","received_events_url":"https://api.github.com/users/jamesdixon/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2016-07-19T18:02:52Z","updated_at":"2016-07-20T16:14:30Z","closed_at":null,"body":"Hi everyone,\r\n\r\nI'm admittedly new to the testing game and trying to better understand the best way to do things. \r\n\r\nThat said, I'm currently using `server.inject` to do some endpoint testing and trying to figure out the best way to stub out a module that is used internally in my handlers. The module in question dumps messages into an Azure Queue, which are then processed by a job which kicks out emails. I obviously don't want to emails to be sent during testing, so I'm looking for a way to prevent that. I believe I could expose said module as a server method and then stub that from within my tests, but I was hoping there might be another way.\r\n\r\nHere's an example, where `EmailNotification` is the module I'm looking to stub out in my tests:\r\n\r\n```js\r\n// handler.js\r\n  /**\r\n   * Ends an appointment.\r\n   * @param  {[type]} request [description]\r\n   * @param  {[type]} reply   [description]\r\n   * @return {[type]}         [description]\r\n   */\r\n    end(request, reply) {\r\n\r\n        const appointmentId = request.params.appointmentId;\r\n        const businessId = request.auth.credentials.businessId;\r\n\r\n        // get the appointment and determine its status\r\n        Appointment.end(appointmentId, businessId, request.payload.data)\r\n        .then((appointment) => {\r\n\r\n            return EmailNotification.appointmentEnded(appointmentId)\r\n                .then(() => reply.jsonapi(appointment));\r\n        })\r\n        .catch((err) => reply(err));\r\n    }\r\n```\r\n\r\nCertainly would appreciate any feedback.\r\n\r\nBest,\r\nJames"},"comment":{"url":"https://api.github.com/repos/hapijs/discuss/issues/comments/233999568","html_url":"https://github.com/hapijs/discuss/issues/316#issuecomment-233999568","issue_url":"https://api.github.com/repos/hapijs/discuss/issues/316","id":233999568,"user":{"login":"maxbeatty","id":93179,"avatar_url":"https://avatars.githubusercontent.com/u/93179?v=3","gravatar_id":"","url":"https://api.github.com/users/maxbeatty","html_url":"https://github.com/maxbeatty","followers_url":"https://api.github.com/users/maxbeatty/followers","following_url":"https://api.github.com/users/maxbeatty/following{/other_user}","gists_url":"https://api.github.com/users/maxbeatty/gists{/gist_id}","starred_url":"https://api.github.com/users/maxbeatty/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/maxbeatty/subscriptions","organizations_url":"https://api.github.com/users/maxbeatty/orgs","repos_url":"https://api.github.com/users/maxbeatty/repos","events_url":"https://api.github.com/users/maxbeatty/events{/privacy}","received_events_url":"https://api.github.com/users/maxbeatty/received_events","type":"User","site_admin":false},"created_at":"2016-07-20T16:14:30Z","updated_at":"2016-07-20T16:14:30Z","body":"Assuming `EmailNotification` is required in `handler.js`, you could use something like [proxyquire](https://www.npmjs.com/package/proxyquire) to stub it out.\r\n\r\n```js\r\nconst proxyquire = require('proxyquire');\r\n\r\nconst Handler = proxyquire('../path/to/handler', {\r\n  'EmailNotification': {\r\n    appointmentEnded: function () { return Promise.resolve() }\r\n  }\r\n})\r\n\r\n// your existing tests with server.inject\r\n```"}}
{"actor":{"display_login":"maxbeatty"}}