What’s the difference between a mock and a stub, and how do you use them in Swift?

(Northern Mockingbird. Public domain.) Lots of people use the terms “mock” and “stub” interchangeably, and there’s a good deal of confusion around them. But mocks aren’t stubs, as Martin Fowler says — they behave a bit differently. So what’s the difference between a mock and a stub, and how do you use them to test your code? Stubbing in Swift …

How to write unit tests in Swift 4 with XCTest

Convinced of the benefits of unit testing for iOS but not sure where to start? Trying to figure out what framework to use, how to set it up, and which test to write first can feel overwhelming. What unit testing framework should you use? XCTest. Here’s why: it works well, it’s easy to set up, and it’s easy to use. …

What are the benefits of unit testing?

When I asked my mailing list “What keeps you from writing unit tests?” one person said “I just don’t really understand the value of them.” And it wasn’t just that person — the majority of responses fit into that category. Generally speaking, iOS developers don’t write unit tests because they don’t know what the benefits are. Let’s look at all …

Not Using Automated Tests: Still a Systemic Problem for iOS Developers

Ash Furrow, over five years ago, in Seven Deadly Sins of Modern Objective-C: Do you unit test your Objective-C? No, you probably don’t. Do you have automated UI acceptance tests for your UI? Nope. Do you have any kind of continuous integration set up? No, you don’t. I don’t understand what is wrong with the Objective-C community that it continuously …

Why iOS developers don’t write unit tests

Last week I asked my mailing list “What keeps you from writing unit tests?” and got more than 30 responses. (If you didn’t participate and want to, just send me an email with your answer to josh@roadfiresoftware.com.) Today I’m sharing the anonymized results, hoping they’ll help all of us become more aware of why we don’t write unit tests — …

What framework should I use to start unit testing on iOS?

If you want to get started quickly, don’t waste your time searching for a good unit testing framework — just use the XCTest framework. It’s built in to Xcode, easy to set up, easy to use, and works well. When you’re writing your first unit tests for iOS, you don’t need the distraction of researching a new third-party library, especially …

What’s the biggest mistake new iOS developers make when coding?

The other day on reddit I found this gem: What are common mistakes self taught programmers make when coding in objective c? This is an excellent question. A developer who goes out of their way to ask what mistakes they might be making is a developer who cares about getting better at coding. And if you care, you’ll get better. …

How to parse JSON with Swift 4

How do you parse JSON with Swift 4? Maybe you’ve done it with Swift 3 before, and if you have, you’ll be pleased to know that it’s so much better in Swift 4. And guess what? Nobody even calls it parsing any more — it’s decoding now. Apple has some new classes and protocols we can use: JSONDecoder and Decodable …

What’s the best way to learn iOS development with Swift?

I hear this question so often that it’s time for another roundup of the best online courses, videos, in-person classes, and books for learning iOS development and Swift. If you’ve looked, you already know there are a ton to choose from. This is a curated list of the ones that are recommended the most, ordered from free to most expensive. …

How to write a singleton in Swift

I’m as opposed to singletons as anyone else in general due to the fact that they make testing harder, but sometimes, like when managing access to a local SQLite database, I want to make sure there’s only one instance of the class that purges old records from it. So a singleton seems to make sense. But how do we do …