People assume you can’t write unit tests for user interface code. That just ain’t so. I’ve already shown you how to do UIViewController TDD. Can we do the same for UIAlertView and UIActionSheet? Sure!

UIActionSheet example

This time instead of a screencast, I’ve put my code on GitHub, because you’ll want to incorporate some classes into your tests. Go to my iOSAlertViewActionSheetUnitTesting repository. Continue Reading…

CocoaConf vs. WWDC

April 22, 2013 — 2 Comments

I just wrapped up a great weekend with CocoaConf San Jose. I’ve gotta say: if you’re looking to maximize bang for your buck, save the money you’d spend on WWDC and consider CocoaConf instead!

cocoaconf

First, there’s the stress of trying to get a WWDC ticket. Then the expense. Sure, there are all those great talks, but Apple makes the videos available to registered developers. I think the main reason to continue going to WWDC is if you’re really desperate to get help in the Apple labs.

I’d never been to another iOS/Mac developer’s conference before this past weekend. I was impressed by:

  • The small size. With only a hundred developers, there were many opportunities to build relationships, even with the speakers.
  • The speakers. This is a class act of experienced people. Many of them are published authors. They’re there because they’re passionate about their craft.
  • The food. Remember when WWDC food used to be decent, before the iOS-induced population explosion? This was like that, only better. Much better.
  • The duration. My brain is full by the third day of any conference. CocoaConf wisely stops there.
  • The price. Way cheaper.

I came away having learned good stuff. But maybe just as important, I made genuine human connections. (Good luck doing that at the great cattle-herding of WWDC.)

CocoaConf is a traveling conference, hitting up several cities in the U.S. You owe it to yourself to check out the one nearest you.

Question: What are your favorite conferences, and why? Leave a comment below.

There are subtle issues around #import order. You may not believe me — until you try reusing code in a new project.

Unfinished Bridge 1” by Jason Thomas, used under CC BY-NC-SA 2.0 / Altered by Jon Reid

In #imports gone wild! we looked at the problems caused by having too many #import directives. But it’s also possible that you have too few, resulting in bad header files — especially if you don’t pay attention to #import order in your .m files.

Minimal and complete

When it comes to imports, header files should satisfy these two conditions:

  • They should be minimal
  • They should be complete

“Minimal” just means a header file should import no more than it needs.
“Complete” means the header file imports everything that needed to compile it. Consider Continue Reading…

The UIViewController TDD screencast ended with all the code in the view controller. Unfortunately, this is where many iOS programmers leave things! Let’s TDD our way to something better…

In part 2, we pick up from there and TDD to extract a model class, which the controller observes. You’ll see it evolve into true Model-View-Controller, driven by unit tests.

In particular, you’ll see how to TDD:

  • a model that posts notifications when it changes, and
  • a controller that observes those notifications.

Continue Reading…

Is TDD worth the extra effort? I got a reaction from one person who tried applying my tips.

TDD thank-you note

Thank You Note” by Roger Carr, used under CC BY-NC-SA 2.0 / Altered by Jon Reid

Andy Dwelly began applying my TDD screencasts to his iOS coding. Here’s what he writes in Some notes on Test Driven Development:

At first progress was almost painfully slow.

Yup. It seems like there’s a lot to learn. The real barrier, I think, is that there is a lot to unlearn. And so, when you first get started with Test Driven Development, your productivity will take a big hit. This is normal! But if you’re willing to press through the learning curve, your productivity will increase again — in ways you may not have experienced before…  Continue Reading…

In my UIViewController TDD screencast, I put IBOutlets and IBActions in the header file. This made them accessible to unit tests, but I knew it would raise questions:

It’s a fair question. There’s a tension between information hiding (don’t reveal things in the interface) and testability (certain things need to be exposed). Exploring that tension leads me to apply the Extract Class refactoring in places I hadn’t considered before. Continue Reading…

In my screencast of UIViewController TDD, I used an example of incrementing a counter and couldn’t help but give a slight aside: “We’ll increment it… ++count, instead of count++, please.”

WHAT YOU SAY!!

<rant>
I blame Apple for propagating poor incrementing style. Programming with Objective-C starts with the statement, “[Objective-C is] a superset of the C programming language.” But every example they give of incrementing a value uses someInteger++, ignoring the fact that C has two different increment operators!

  • Post-increment: count++
  • Pre-increment: ++count

The Wikipedia article Increment and decrement operators gives clear examples of the difference.

What performance impact will it make? None. The extra code for post-increment semantics is probably optimized away by today’s compilers. Even if it’s not, it’ll still have almost no impact. So what does it matter?

It matters to the reader. It matters because it shows a lack of familiarity with the language’s operators, which doesn’t fill me with confidence in your code. It matters because Objective-C Is Still C (Not Java!)

Say what you mean, rather than relying on side-effects.
</rant>

Question: Do language subtleties really matter? Leave a comment below.

This screencast focuses on the question I get the most: “Do you do test-driven development for view controllers?” It’s clearly a roadblock for many people. This screencast should remove that roadblock.

It also answers the question, “Is it worth doing?”

Outline:

  • Three types of unit test verification
  • View controller unit tests: the trick
  • TDD demo
  • How UIViewController TDD can actually help you code faster

Continue Reading…

TDD is a series of small steps. It can be difficult to grasp until you see those steps demonstrated. That’s why I made this screencast. It was sparked by a Stack Overflow question that said, “All the examples of unit testing I read about seem to be extremely simple and trivial.” The question asks how to write unit tests for a piece of sample code. What’s interesting about this problem is that it uses NSUserDefaults.

Outline:

  • What to do with an external object (in this case, the NSUserDefaults singleton). Dependency injection to the rescue!
  • My basic setup: OCUnit + OCHamcrest + OCMockito (along with my test case template and test code snippets)
  • The 3 steps of “The TDD Waltz”
  • We TDD our way to the desired functionality

Continue Reading…

One of the first things I do when working on any Xcode project is set up code coverage. If the coverage shows a hole, I know that area is lacking unit tests.

Code coverage hole in the road

Sinkhole” by Salim Virji, used under CC BY-SA 2.0

(Be careful, the opposite isn’t true: Just because some code has been touched by unit test execution doesn’t mean it’s actually covered. If altering the behavior of the code causes a test to fail, then you know it’s covered.)

Many people use CoverStory, a a code coverage browser app written by my friend Dave MacLachlan. Others use gcovr to integrate code coverage into their Jenkins continuous integration. Me, I use lcov because it lets me exclude third-party libraries from the measurements before generating an HTML report. Continue Reading…