Quality Coding
Shares

All posts by Jon Reid

In Defense of Uncle Bob’s Dark Path: Swift and Testability

Uncle Bob set off another firestorm with his blog post The Dark Path. Condemnation from the Swift programming community was, well, swift. How dare he insult our wonderful new language? Clearly he’s a n00b who hasn’t done enough Swift programming.

Emperor Palpatine: "Good, good, let the hate flow through you"

Except that he’s no n00b — not even close. As Mark Seemann said,

I’m here to defend Uncle Bob’s post, as it relates to unit testing and TDD.

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

Your TDD Will Improve as Your Design Sense Improves

Shares

When you’re new to Test-Driven Development, there’s a strong tendency to continue writing code the way you used to. Then you step back and ask, “But how can you unit test this? TDD is so impractical!”

Almost always, my answer is: “Then don’t write it like that.”

Apple recently published a blog post, Working with JSON in Swift. I was looking for tips on Swift JSON parsing, and found the beginning of the article quite helpful. But it took a bad turn at the end…

Continue reading

Have You Wished You Could Fake a Return Value in Swift?

Shares

I’ve shown you how to replace real objects with fakes in Swift, so that you can begin creating useful mock objects. We did this by moving away from a concrete type to a protocol. Anything that implements the protocol will work, so fakes are no problem.

But what do we do when we can’t change the signature? How do we substitute fake arguments, or a fake return value?

It’s time to talk about partial mocks in Swift.

Continue reading

How to Design Swift Mock Objects

Shares

Last time, we looked at How Does Swift Support Stubbing and Mocking? Swift’s extension declaration opens the way, letting us attach new protocols to existing classes. That much is well-known in the Swift community.

But then what? How do we create useful mock objects? I’ve seen many examples that are okay… to start with. But in the long run, I’m afraid they’ll fall short for ongoing maintenance.

I wrote OCMockito because I wanted Objective-C mock objects that struck a better balance between “precise” and “forgiving”. I also wanted test code that was easier to read and write. What can we do with Swift mock objects?

Continue reading

How Does Swift Support Stubbing and Mocking?

Shares

You’re writing a unit test in Swift. You feel the need to avoid interactions with a real object. You want to provide a fake object, or a “test double” (like a stunt double). But Swift is strict about types! What do we do?

Continue reading

I Found a Design Pattern for Tests that Do Real Networking

Shares

I try to avoid asynchronous tests because they’re slow. But sometimes they’re worth having. When you do have them, it’s worth spending extra effort to make them as fast as possible.

My mistake was that I only focused on the speed of the happy path. Today, I learned a way to improve the speed of failure scenarios, at least around testing network requests.

Continue reading

I’m Pretty Sure Most of Us Are Wrong about XCTestCase tearDown…

Shares

Since 2001, I’ve relied on an understanding of test execution flow in xUnit frameworks. But somewhere along the way, my understanding of the XCTestCase life cycle got messed up. I picked up an assumption that’s just wrong.

At best, it’s an assumption that can bloat our test runs. At worst, it can wreak havoc.

When is an XCTestCase deallocated?

This is the big question. I had it right for a long time, but then I lost it.

Continue reading

Where Will Jon Be in Fall of 2016?

Shares

Do you enjoy conferences and workshops? I’m looking forward to attending, teaching, and speaking at these events in the fall of 2016…

Developing a Design Sense for Your Code

First up is something I’m attending.

Continue reading

How Can Swift Language Features Improve Testability?

I learned how to write testable code in C++ and Objective-C. But what about Swift?

The features and the overall “feel” of a language can have a big impact on how we express our code. Many of you know this well, because you started Swift early, and are miles ahead of me. I’m delighted to play catch-up. Swift’s features are like new toys! …But how do they affect testability?

Continue reading