Skip to content
This repository has been archived by the owner on Dec 5, 2019. It is now read-only.

Alternatives #1

Open
Raynos opened this issue Oct 10, 2012 · 13 comments
Open

Alternatives #1

Raynos opened this issue Oct 10, 2012 · 13 comments

Comments

@Raynos
Copy link

Raynos commented Oct 10, 2012

what do you use to do this kind of thing that is not this?

@dominictarr
Copy link
Owner

oh, I don't do this kind of thing anymore.

this was from my old mad-science project, that I've back burnered.
I was using this to swap different versions when running tests...
Even when I was working on it,
I realized that you can do this stuff more cleanly by installing
the right node_modules with the exact versions you want to test.

@Raynos
Copy link
Author

Raynos commented Oct 10, 2012

@dominictarr what do you use to unit test some code and mock out dependencies that touch the database?

@dominictarr
Copy link
Owner

databases are antipattern.

@dominictarr
Copy link
Owner

mocking is an antipattern too.

if you really think you need that... build injection into your module.

module.exports = function (evilDbThing) {
  return {
    ...
  }
}

and then go require('db-toucher')(db)

@Raynos
Copy link
Author

Raynos commented Oct 10, 2012

@dominictarr

databases are antipattern

#winning

@Raynos
Copy link
Author

Raynos commented Oct 10, 2012

@dominictarr that's a pain in the ass because the database is everywhere >_<

And why inject variables into function. That's what require does

@dominictarr
Copy link
Owner

well, if you want to use different things with the same interfaces... it's better to be explicit that some wacky hack.

@Raynos
Copy link
Author

Raynos commented Oct 10, 2012

@dominictarr having looked at all the ways to be explicit. Using require is the best and cleanest.

@dominictarr
Copy link
Owner

"database is everywhere" is probably the root of the problem.

alternatively, you could just have a thing

  require('db')({mock...})

do that first, and it could set a global thing that returns the database the rest of the time...

@Raynos
Copy link
Author

Raynos commented Oct 10, 2012

@dominictarr we have a bunch of repo files that have all the db stuff in them. Everyone requires the repo. I dont want to have everyone pass the mock thing through themself into the repo.

I just want to swap everyones repo out for a mock one and not have to pass in the repo.

It's about

var repo = require("./repo")

module.exports = function myThing() {
    ...
}

vs

module.exports = function myThing(repo) {
    ...
}

It's just a pain in the ass to pass everything in. Last time I did that I wrote a dependency injection framework called ncore.

@Raynos
Copy link
Author

Raynos commented Oct 10, 2012

@dominictarr the global db backdoor to punch the mongo library and inject a fake db thing is a hack.

I prefer proxyquire("/thing", { repo: {mock...} })

@dominictarr
Copy link
Owner

@Raynos, you must make you own decision and live with it.

@Raynos
Copy link
Author

Raynos commented Oct 10, 2012

@dominictarr remind me to not do the "ask your for advice then tell you, you are wrong and then argue with you about you being wrong" again.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants