Category Archives for iOS TDD

The 3 Laws of TDD: Focus on One Thing at a Time

Shares

When I was first learning TDD, I’d try to get to the First Step (a failing test) by writing a fully-formed test. But it often took a lot fiddling to get that test to run and fail. Sometimes this was because the production code took several steps to set up. Sometimes it was because the test code wasn’t right.

One of the tricky parts of TDD is that we’re creating two streams of code in parallel: the test code, and the production code. Two things changing at the same time… that’s a hard thing to keep in your head!

Continue reading

What Can We Learn from an Incorrect TDD Diagram?

Shares

A paper published in 2013 about Test Driven Development included the following diagram. Unfortunately, it gets some things wrong:

incorrect TDD diagram

Continue reading

JSON Parsing: One of the Easiest Places for TDD Beginners

Shares

How can we use Test-Driven Development for JSON parsing? Most developers are concerned with ways to implement the production code. There are various approaches, various libraries… But what about the unit test side of things?

If we write effective unit tests, the design can appear incrementally. And with a strong suite of tests, you’re free to change the implementation — even radically. When the results are guaranteed by tests, whatever approach you take or library you use becomes an implementation detail!

Last time, we looked at design principles, such as sticking to the Single Responsibility Principle and returning a Response Model. This time, let’s look at:

  • Test principles
  • A TDD demo in Objective-C

Continue reading

TDD Sample App: The Complete Collection …So Far

Shares

When I began my TDD Sample App, my hope was that it would help us explore a number of topics around TDD and Clean Code.

On one hand, the app itself has barely progressed. However, the blog posts cover a surprising variety of topics.

Continue reading

Static Analysis: Will It Free You from TDD?

Shares

There’s been a growing debate about static languages vs. dynamic languages. For me, it started with Uncle Bob’s Type Wars, but it has expanded into many circles. Writing my own reaction helped me discover that I was unfairly judging Swift based on C++.

In the midst of the debate, another interesting article popped up: Eric Elliot’s The Shocking Secret About Static Types. It points to a couple of studies. One concludes that there is a lack of evidence that static typing reduces defect density. The other more rigorous paper concludes, “There is a small but significant relationship between language class and defects.”

Check out Eric’s fascinating conclusion:

When it comes to bug reduction, I think it's fair to say: Static types are overrated. But when it comes to other features, static types are still cool, and may still be worth using. Bottom line: You want to reduce bugs? Use TDD. You want useful cod intelligence tools? Use static types.

We’ve come full circle: a discussion about static types has again brought us back to TDD. Why?

Continue reading

How a TDD Mistake Revealed an Error in My Design

Shares

My TDD has improved since I first started in 2001. But even today, I make mistakes. The trick is to learn to recognize TDD mistakes. Then, learn to “listen” to them: what is it trying to tell me about the design?

Follow along as I recount the latest steps in Marvel Browser, the iOS TDD sample app. Can you spot the errors before I point them out?

Continue reading

Can TDD Be Simple? With Preparation, Yes

Shares

TDD doesn’t make anything happen automatically. You really need to level up on two other skills as you go: design, and unit testing. Doing so can shift TDD from being daunting to being simple.

Let’s look at how a change to unit testing empowers TDD.

Continue reading

3 Easy Steps to Refactoring Tests for Greater Clarity

Shares

I’ve given you 3 Reasons Why It’s Important to Refactor Tests. But what’s actually involved in refactoring tests?

Disclosure: The book links below are affiliate links. If you buy anything, I earn a commission, at no extra cost to you.

The definitive book on this topic is xUnit Test Patterns: Refactoring Test Code by Gerard Meszaros. It’s a big book, full of patterns and smells. But there are 3 simple steps I take most often.


This is a five-minute screencast where I’m refactoring tests in my iOS TDD sample app using those 3 steps:

Continue reading

3 Reasons Why It’s Important to Refactor Tests

Shares

You may have seen the 3 steps of “the TDD waltz”: Fail, Pass, Refactor. There are many ways to do it wrong! Two common mistakes are:

  • Skipping the refactoring step
  • Not skipping it, but refactoring only production code

So let me give you 3 reasons why it’s important to refactor tests.

Continue reading

TDD Networking with DI: Is It Really That Easy?

Shares

Can you TDD networking requests? Sure! It’s just a matter of using Dependency Injection (DI).

But first, a quick recap. Remember this design?

Web request: Clean Architecture

We want a Service class. Now when I began using this style, I made a mistake: I created a single Service class to house an entire API. This violates the Single Responsibility Principle.

The Marvel Browser may end up supporting only one API call. But I’m afraid naming it MarvelService would lead people down the wrong road. We are fetching comic book characters. So let’s use a narrower name: FetchCharactersMarvelService.

Remember: Smaller, focused classes are easier to manage than larger, godlike ones.

Let’s TDD it!

Continue reading

>