Permalink
Browse files

0.2.0 added overrides and avoids caching when overriding

  • Loading branch information...
seanhess committed Aug 28, 2012
1 parent 521f8e6 commit 74a0d095cfe6db725d9b4e97f4d6a7f9c12b0b37
Showing with 34 additions and 3 deletions.
  1. +23 −2 README.md
  2. +1 −1 package.json
  3. +10 −0 test.coffee
View
@@ -102,15 +102,36 @@ app.coffee
### Using Dependable's Load
-You can load files or directories instead of registering by hand. See below.
+You can load files or directories instead of registering by hand. See [Reference](#reference)
+
+### Overriding Dependencies for Testing
+
+When testing, you usually want most dependencies loaded normally, but to mock others. You can use overrides for this. In the example below, `User` depends on `Friends.getInfo` for it's `getFriends` call. By setting `Friends` to `MockFriends` we can stub the dependency, but any other dependencies `User` has will be passed in normally.
+
+ # boostrap.coffee
+ deps = container()
+ deps.register "Friends", require('./Friends')
+ deps.register "User", require('./User')
+
+ # test.coffee
+ describe 'User', ->
+ it 'should get friends plus info', (done) ->
+
+ MockFriends =
+ getInfo: (id, cb) -> cb null, {some:"info"}
+
+ User = deps.get "User", {Friends: MockFriends}
+ User.getFriends "userId", (err, friends) ->
+ # assertions
+ done()
## Reference
`container.register(name, function)` - registers a dependency by name. `function` can be a function that takes dependencies and returns anything, or an object itself with no dependencies.
`container.load(fileOrFolder)` - registers a file, using its file name as the name, or all files in a folder. Does not follow sub directories
-`container.get(name)` - returns a module by name, with all dependencies injected
+`container.get(name, overrides = {})` - returns a module by name, with all dependencies injected. If you specify overrides, the dependency will be given those overrides instead of those registerd.
`container.resolve(cb)` - calls cb like a dependency function, injecting any dependencies found in the signature
View
@@ -1,6 +1,6 @@
{
"name": "dependable",
- "version": "0.0.3",
+ "version": "0.2.0",
"main": "index.js",
"author": "Sean Hess",
"repository": { "url": "git@github.com:idottv/dependable.git" },
View
@@ -171,6 +171,16 @@ describe 'inject', ->
deps = container()
assert.equal deps.get('_container'), deps
+ describe 'cache', ->
+ it 'should re-use the same instance', ->
+ deps = container()
+ deps.register "a", -> {one: "one"}
+ a = deps.get "a"
+ assert.deepEqual a, {one: "one"}
+ assert.notEqual a, {one: "one"}
+ a2 = deps.get "a"
+ assert.equal a, a2
+
describe 'overrides', ->
it 'should override a dependency', ->
deps = container()

0 comments on commit 74a0d09

Please sign in to comment.