How to Use Dependency Injection to Make Your Code Testable

January 21, 2016 — 1 Comment

If we’ve learned the Three Steps of TDD and the Three Laws of TDD, what keeps us from doing Test Driven Development? Maybe it’s not knowing how to write unit tests. Or more specifically, not knowing how to write unit tests against “real” code.

And what keeps us from writing unit tests against our own code? I bet it’s not knowing what our many options are.

Dependency Injection can unlock your code.

Unlock Your Code with Dependency Injection

Birth of a presentation

That was the thought process I had when I was first asked to speak at a technical conference.

My talk Test Driven Development for iOS (and anything) …by Controlling Dependencies at MCE 2014 in Warsaw could have crashed and burned:

  • It had an awkward title.
  • It wasn’t really about TDD.
  • I’d never spoken at a conference.
  • I uttered many ums and ahs, and touched my mouth nervously.

But it was rated “Best Talk at MCE”!

Watch “Controlling Dependencies”

Here’s a rough outline:

  • 3 Types of Unit Tests
  • How Interaction Tests Work
  • 4 Types of Dependency Injection
  • Example: Writing Tests for Networking Calls (using a hand-rolled mock)
  • Refactoring Test Code for Readability

When have an hour, pour yourself a drink and watch. Or take it in sections. The talk itself is 42 minutes, followed by Q&A.

Here are the books on my last slide:

In addition to the video, I’m making my slides available as a bonus. You can access the bonus PDF here.

Access Bonus PDF

What successes have you had applying Dependency Injection to your code? Did it help you test something that used to resist testing? Did it help in other ways? Click here to share your story!

Jon Reid

Posts Twitter Facebook Google+

I've been practicing Test Driven Development (TDD) since 2001. Learn more on my About page.

One response to How to Use Dependency Injection to Make Your Code Testable

  1. great talk. We use Dep-Inj and this give the opportunity to test classes that otherwise can be hard to test.
    We use JSObjection.

Leave a Reply

Text formatting is available via select HTML.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> 

*