Starting a new journey

Testing Categories in Objective-C

Ana Nogal

I’m a senior iOS developer, with several years of experience and more than 20 years in software development. I spent the first 15 years working as a back-end developer in .Net and Java, but I fell in love when I discovered Mobile development. I am passionate about the quality and maintainability of code and application architecture.

Contact Info
Mail Address

Yesterday was katas day: the plan was to start the day with a simple one and then move to another a little more difficult. That was a good plan.
So I decided to do the Stack: a very simple kata, I just had to simulate a stack and have the following methods:

- (void) push: (id)object;
- (id)pop;

And for rules just this one: “stack should throw an exception if popped when empty.”

Simple right? I thought so too. And it was really easy to solve it: just create a NSMutableArray category and add the methods to NSMutableArray.

So I create I first test :

- (void)testShouldPushAnObjectTotheStack {
   NSMutableArray *stack = [NSMutableArray array];
   [stack push:@"Pushed an object"];
   XCTAssertTrue([stack[0] isEqualToString:@"Pushed an object"]);

So I create my class and my code and everything builds! Great, just run the tests and that’s it! No it isn’t!!! Here’s the result of my test:

What? So the code builds and then I have this error:”unrecognized selector sent to instance”. Well let’s ask google….
In reality it took me all morning to figure it out! I must improve my search techniques!!! After googling a lot and a lot of frustration, I find a blog post that saved my day: So if you want to test a category you must add -all_load flag to your test project. Go to: YourTestTarget > Build Settings > Linking > Other Linker Flags.

Problem solved!
You can find my final solution here.

Leave a Reply