Live data from Hacker News

You don't inject for tests

blog.arkency.com

11–14 of 14 posts

Re: You don't inject for tests

#13
Let me turn this around.

In a dynamic language, unit testing is a bad reason to use dependency injection. There are lots of valid reasons to use it. But you don't need it for unit testing.

Returning to DHH's example, if you just want to get the current time in Ruby code, just get it. You can use the dynamic nature of the language to stub that out and unit test it. But you don't need to use dependency injection to enable that. And you really don't need to use a heavy framework.

If you have a different reason to use dependency injection, though, by all means go ahead.

Re: You don't inject for tests

#14
post #11

I test to verify that my code works.. Why would unit test show me where object orientation breaks down?

When things are hard to unit test, it might be a smell that there is something wrong with your OOP code.

But that's why you start by writing the tests and then evolve the test and the production code in parallel, so that you don't end up with code that is hard to test. I don't think that just because something is testable means that the design incorporates good OO practices.
Post reply on HN