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:
- Add MyAppTesting unit test bundle target.
- Under get info on MyAppTesting, add MyApp as Direct Dependency.
- Create a group called "Tests".
- Under "Tests", add ObjectiveC Test Case Class.
- 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…
- test target's
Bundle Loader
build setting is pointing at your app's bundle. - test target's
Test Host
build setting is pointing at your app's bundle. - app target's
Symbols Hidden by Default
build setting isNO
.
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.
精彩评论