No it's not hardcoded. I'll try to be more clear.

Lets implement auth module as follows:

auth/auth.js
auth/test.js
auth/index.js

//auth/auth.js
module.exports = function setup (users) {
  return  function authenticate (email, password, cb) {
    // using user repo here
  }
}

//auth/test.js
var users = require('user-repo-stub')
var auth = require('./auth')(users)
// tests go here

And know we came to the real problem. How to supply user repo to each 
location where auth module required? Just exposing auth/auth.js is 
(probably) ok if we need to require it only one-two times. But what if we 
need this module in many unpredictable places? Possible solution is:

/auth
/user-repo
/another-module-probably-going-to-use-auth-or-user-repo
/node_modules/auth.js
/node_modules/user-repo

//node_modules/auth.js
var users = require('user-repo') // Ready to use user repo
module.exports = require('../auth')(users)

Then any module in auth's dir can require already initialized instance of 
auth function just doing - require('auth'). 

That's all. We can invent many variations of pattern above and dependency 
management in general. But that's all about the rule - do not create 
required objects within application code. Any module should receive ready 
to use instance of required object




On Saturday, March 24, 2012 1:23:18 PM UTC+4, Martin Lundberg wrote:
>
> On Saturday, March 24, 2012 6:29:17 AM UTC+1, Eldar wrote:
>>
>> For now I would refactor your module in this way:
>>
>> // authentication
>> var repo = require('user-repo')
>> module.exports = function authenticate (email, password, cb) {
>> ...
>> }
>>
>
> Wouldn't these changes make it harder to unit test the auth.js file since 
> it's hardcoded to use the user repository module. How would I stub it?
>

-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply via email to