Dependency Injection and a Cocoa Framework

Discussion in 'iOS Programming' started by ZaBlanc, Jun 25, 2009.

  1. ZaBlanc macrumors newbie

    Apr 28, 2009
    I've messed with iPhone dev for a few months now, and my assessment is three things:

    1) Great tools, including debugging, performance testers, and XCode/IB.
    2) Weak Unit Testing framework. No async support. Very frustrating to get compiling right.
    3) No dependency injection.

    The unit testing stuff annoys me the most because I'm a TDD guy, but unit testing for iPhone apps is beyond frustrating. First, if you have no tests yet, you get a darn test failure. WTF? Second, the results are in the build process. Clunky. Third, async testing doesn't work. Fourth, I'm often puzzled why things aren't compiling. Adding frameworks, which is what I tend to suspect is the problem, only makes more errors. And, finally, the big number five, pooooooooor documentation on the whole process and when you find some, it's likely an outdated version of XCode.

    And where the HELL is my green bar? :)

    I have debated writing a better, runtime unit testing framework. However, I am also eyeing doing some research into constructing better iPhone apps. In my experience of writing 5 different apps, it seems that we're resigned to using Singleton models (BLECH!) and no dependency injection. And, no, while leveraging the delegate gets us past many problems, it is only a's bad code to make everything depend on [[UIApplication sharedApplication] delegate].

    So, I have two questions:

    1) What solutions have you come up for these problems?
    2) Is it worth my time to research and come up with my own?

    Would LOVE some feedback on this.
  2. kainjow Moderator emeritus


    Jun 15, 2000
    Async tests can be done with some some NSRunLoop magic. If you call a method and are waiting for a delegate callback, you can use [NSRunLoop runUntilDate:] to wait. Kind of hacky but it works.
  3. Luke Redpath macrumors 6502a

    Nov 9, 2007
    Colchester, UK
    Couldn't agree more on the unit testing front; I too an a TDDer and have been spoilt by all of the great tools available for Ruby (my other language of choice).

    I've tried running the build output through a Ruby script that prints out more a more familiar test output and wrapped it up using a Rake task but its still a bit rough; you can find my efforts on Github.

    But yes, I find the whole process clunky and Objective-C syntax doesn't really lend itself to an elegant testing framework.

    I'm kinda hoping MacRuby might be at some point a suitable means of testing native Objective-C code, but with all of the great Ruby test frameworks available to use.

    One thing I have found that makes the tests slightly more readable is the hamcrest matchers for Objective-C.

    Here's an example test of mine written using them:

  4. Luke Redpath macrumors 6502a

    Nov 9, 2007
    Colchester, UK
    Regarding dependency injection, its not something I've really used much (again it's not really necessary in Ruby-land) and I've managed to get by without it in Objective-C with a limited use of singleton objects (but I avoid abusing the application delegate as one big global god class).

    For instance, you could have some kind of ServiceLocator class that is a singleton and can be used to create instances of objects that have complex creation requirements and return them to you.
  5. ZaBlanc thread starter macrumors newbie

    Apr 28, 2009

    I'm sort of a purist, so I'd like to see XCode be able to handle unit tests and the sort without magic. Ruby is great, but native tools should be fine.

    At this point, my concerns seem confirmed (no magic framework out there I wasn't aware of). At this point, the decision seems to be which framework to tackle. I think getting some unit tests working, minus the async stuff, involved more understanding of what I'm not including on my Unit Test target rather than actually writing the test. Dependency injection seems more critical...and I have some ideas of how to sneak it in (Categories will come in handy dandy.)

    Please, more feedback would still be great if you're reading this thread.

    Any Flex devs out there ever use Mate? That's what I'd like to see.

Share This Page