views:

880

answers:

2

Starting with an app already in development, I have carried out the instructions in the iPhone Development Guide – Unit Testing Applications

I can successfully include and use my App's classes in Application-style tests that run on the device, and output their results to the console.

If I add the following line of code:

STAssertTrue([viewController isKindOfClass:[LoginViewController class]], @"Top view controller is not LoginViewController");

The following build error is generated:

Undefined symbols:
  "_OBJC_CLASS_$_LoginViewController", referenced from:
      __objc_classrefs__DATA@0 in LoginViewTest.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

I can provide more configuration information for the project and the Testing target, but the setup works file without the [LoginViewController class] line in the test source.

Without that line, I can reference the class, use it's properties and send it messages successfully.

Is there a linking build setting, or bundle loading option that is required when attempting to use an App class in this fashion? Or should I find another type of test to confirm that the class of an object is the expected one?

+3  A: 
ohhorob
+1  A: 

I just answered this over here:

http://stackoverflow.com/questions/2013118/iphone-unit-testing-symbols-not-found-when-calling-custom-code

I suppose one of these should be closed as a duplicate? I'm not reputable enough to do so...


I also followed Apple's iPhone Unit Testing Applications document and saw a linking error similar to the one described in the question when trying to unit test one of my classes.

Looks like any class referenced in your unit test class and so being run from your test target also needs to be added to that test target. To do this, you would right click your RootViewController class and click 'Get Info' (Cmd-i shortcut). On the targets pane, make sure your unit test target (e.g. 'LogicTests', if you've followed the naming in that document) is checked.

Now that class will be compiled with your tests and should be available to your unit test. To double check, expand the 'Targets/LogicTests/Compile Resources' node in the 'Groups & Files' browser on the left. This lists all the class files available when building the target and should now include your unit test class together with your class under test.

(Note that you'll need to similarly select all appropriate targets when you create a new application or test class - on the same page of the 'New File...' window when you name the file).

(I'm using XCode 3.2.3 & OS 4.0, by the way).

Martin Dow
Your notes are generally OK. But they don't address the question I asked. My question and answer deals with the symbols being available from the injected unit test bundle at link time. This is also the error indicated by the other question you link to. Your answer is an important part of setting up the "testing" target, but in both our cases that was not causing any problems.
ohhorob