Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

_OBJC_CLASS_$ Errors While Unit Testing Custom Classes In iOS 4.2

Alright, I've spent about 5 hours trying to figure this out. Absolutely none of the past Stackoverflow topics resolutions worked for me, so I'm hoping someone can give me an answer and not a wild goose-chase.

Problem: I have an x-code project that needs unit-testing of my custom classes. I'm using X-Code 3.2.5 with iOS SDK 4.2. After following several different ways, I cannot get my unit-testing working on custom classes. It works fine using Apple's examples.

The custom classes are simple sub-classed NSObjects with trivial iVars. We'll call the class "Snookie".

I've already attempted several questionable resolutions, but would like a response from someone who has had the exact same issue, with an answer that makes sense.

Replication:

  1. Add MyAppTesting unit test bundle target.
  2. Under get info on MyAppTesting, add MyApp as Direct Dependency.
  3. Create a group called "Tests".
  4. Under "Tests", add ObjectiveC Test Case Class.
  5. In the new test case class .h, import Snookie.h, and create an iVar:
#import <SenTestingKit/SenTestingKit.h>
#import <UIKit/UIKit.h>

#import "Snookie.h"

@interface SnookieTests : SenTestCase {
  Snookie *snookieObject;
}
@end

In the new test case class .m, alloc/init snookie as follows:

#import "SnookieTests.h"


@implementation SnookieTests

- (void) setUp
{
  snookieObject = [[Snookie alloc] init];
}

- (void) tearDown
{
  [snookieObject release];
}

@end

The error:

"_OBJC_CLASS_$_Snookie", referenced from:
Objc-class-ref-to-Snookie in SnookieTests.o
Symbol(s) not found
Collect2: Id returned 1 exit status
like image 368
Richard Avatar asked Feb 26 '23 16:02

Richard


2 Answers

What think linker is trying to tell you is that it cannot find an @implementation for the Snookie class in MyAppTesting or any of the frameworks/libraries it links.

Adding MyApp as a directly dependency is not sufficient to tell Xcode to compile/link the code from MyApp. You need to explicitly add the Snookie.m file to your target for MyAppTesting.

like image 115
kperryua Avatar answered Mar 27 '23 11:03

kperryua


While adding your app's .m files directly to your testing target solves the problem, it's redundant and unnecessary. Follow the steps outlined here by Two Bit Labs to get it working. To sum up, make sure your…

  1. test target's Bundle Loader build setting is pointing at your app's bundle.
  2. test target's Test Host build setting is pointing at your app's bundle.
  3. app target's Symbols Hidden by Default build setting is NO.
like image 26
jemmons Avatar answered Mar 27 '23 12:03

jemmons