12

I created a new Cocoa Touch Static Library project with Unit Testing in XCode 4 and added a category:

// NSString+Inflections.h
@interface NSString (Inflections)
- (NSString *)pluralize;
@end

// NSString+Inflections.m
@implementation NSString (Inflections)
- (NSString *)pluralize { return self; }
@end

then added the appropriate import statement to my test cases and wrote the following test:

- (void)testPluralize
{
  NSString *test = @"person";
  NSString *expected = @"people";

  NSString *actual = [test pluralize];

  STAssertEqualObjects(actual, expected, @"Whoops"); 
}

However, this causes my tests to crash (not fail) with 'unrecognized selector sent to instance'.

How can I test a category inside a library?

I've compressed and uploaded the full project here if my description is inadequate.

Kevin Sylvestre
  • 34,782
  • 30
  • 138
  • 226

4 Answers4

19

I was searching for an answer this problem myself and found (I believe) a simpler solution, which doesn't require remembering to add a reference in the Compile Sources list whenever a new category class is added to the project.

In the test target's build settings, add -ObjC to the Linking / Other Linker Flags value.

Further explanation for why this error actually happens can be found at Apple Reference.

Bill
  • 3,646
  • 5
  • 30
  • 45
13

Edit: Be sure to see this answer about unit test not finding files

+1 For uploading the project. The problem is your test target does not include NSString+Inflections.m in the Build Phases.

In XCode 4

  1. Click "Poppy" in the top of the Navigator
  2. Select "PoppyTests" under Targets
  3. Go to Build Phases
  4. Expand Compile Sources
  5. Click the + sign and then add NSString+Inflections.m

You should then get something similar the following output

TestSuite '/blahblah/PoppyTests.octest(Tests)' finished at 2011-03-28 21:31:34 +0000. Executed 1 test, with 1 failure (0 unexpected) in 0.000 (0.002) seconds

Community
  • 1
  • 1
Joe
  • 55,599
  • 8
  • 122
  • 132
  • 3
    Thanks. That fixed the problem, but PoppyTest target does have Poppy as a dependency. I did a bit more looking and found that it can also be fixed by adding `-all_load` as a Other Linker Flag for the PoppyTest target (under Build Settings). This way one wouldn't need to add all source files to both targets. – Kevin Sylvestre Mar 28 '11 at 21:44
  • 1
    That is correct I did not see that, I was thinking it was an `-all_load` kind of issue at first because you were working with a category. I would recommend `-force_load [dependency name]` if it were not a unit test because that option will only load all symbols in the specified dependency. – Joe Mar 29 '11 at 13:25
  • This fixed it for me. The odd part was it was working fine and when it stopped working for me it was using the old source files. – robhasacamera Jul 05 '12 at 21:19
  • 1
    @KevinSylvestre How do you set the -all_load flag? Is it somewhere in Build Settings? – robhasacamera Jul 05 '12 at 21:34
  • 2
    Bill's answer below is much better than this one. You should not manually add your library's source files to the unit test target. Every time you do, a kitten cries. You should link your unit test against your library instead. Every time you do that, a kitten smiles. – Shaggy Frog Apr 04 '13 at 02:54
1

I can also confirm that adding the required files to the Test project's Build Phases > Compile Sources collection solves this problem. Thanks a bunch!

When I tried the -all_load approach, however, I started to receive a bunch of errors, like that _CGSizeZero is undefined for architecture i386. It turns out that some code - like this one, where I use CGSizeZero in one of my classes - can make the second approach break.

Once I replaced CGSizeZero with CGSizeMake(0, 0), however, the second approach worked great as well.

Daniel Saidi
  • 5,723
  • 4
  • 23
  • 27
1

I had the same situation when i was working in the Objective-c project. I did the following steps and it went well.

1) Select the project and goto target.

2) select Test target and add -ObjC like showing in the image.

enter image description here

3) Now try to run the unit XCTest.

Zumry Mohamed
  • 7,994
  • 5
  • 40
  • 48