开发者

_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 n开发者_JAVA技巧ot 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


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.


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.


kperryua is correct.

In XCode 5, click on your class-to-be-tested implementation file, (e.g. XYZCustomClass.m), make sure you can see the Utilities screen (which can be displayed under View > Utilities > Show Utilities, if hidden), and then check the [your tests name] Tests checkbox under Target Membership box. Voila, your unit tests "OBJC_CLASS_$ . . .", referenced from: objc-class-ref in . . ._Tests.o" error message will disappear.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜