TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

You don't inject for tests

19 点作者 persei8超过 12 年前

8 条评论

wyuenho超过 12 年前
I think we are starting to argue about semantics here. This example is simply a very common way of coding to an interface, which BTW, duck-typing makes this so much easier.<p>I think through out the whole discussion about DI in the past week or so is that people don't talk about the injectors themselves enough. Too many pseudo-experts talking about inconsequential and totally unrelated programming principles and paradigms.<p>I don't think people naturally object to coding to the interface, which this example illustrates very well its utility. I think detractors simply just hate the injectors, the people who use the bad injectors indiscriminately, and the stubborn people who insist on doing DI on an entire app.<p>My guess is, most people automatically jump to a DI framework not really because of DI, you can do DI with just a good old factory and may be a good configuration library. Most people jump to DI because of a framework, and the niceties that said framework offers. After a while, people just start confusing the effect with the cause.<p>Can we please start talking about what is DI, when would one find it useful, and what DI approach is suitable under what circumstances now?
RyanZAG超过 12 年前
Can someone not work out a way to 'unit test' dependency injection vs other methods so we have some facts to compare? These discussions feel like people arguing with each other over which color is the best, with everyone chiming in with random anecdotes and feelings.<p>Red must be the best - it goes the fastest!
btilly超过 12 年前
Let me turn this around.<p>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.<p>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.<p>If you have a different reason to use dependency injection, though, by all means go ahead.
zby超过 12 年前
It's like removing magic constants from business logic into config files :)
评论 #5031772 未加载
fideloper超过 12 年前
I "discovered" DI when learning about Unit Testing, but the benefits to maintainability were obvious from the get-go.<p>I'm not sure many people argued that it wasn't about maintainability. I'd say they just ignored that part of the argument.<p>The best explanations of Unit Tests I've seen also explain how it helps improve maintainability, so perhaps that's why Unit Testing and Maintainability has always gone hand-in-hand in my thinking.
mimog超过 12 年前
I test to verify that my code works.. Why would unit test show me where object orientation breaks down?
评论 #5032337 未加载
franzwong超过 12 年前
It would be quite hard to draw a straight line to separate goals of software creation and maintenance.
martinced超过 12 年前
No need to read such a post...<p>Title says: <i>"You don't inject for tests"</i><p>And, before opening the link, you know what you're going to find.<p>Someone confusing "testing" and "unit testing".<p>First thing that came to my mind (while reading the header on HN) was <i>"There are several kind of testing..."</i> but... Curiosity killed the cat. Let's open the link.<p>Ouch, first sentence:<p><i>"What is unit testing for?"</i><p>OK, closed.<p>Don't waste your time reading this crap ; )
评论 #5031971 未加载
评论 #5032041 未加载