Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
761 views
in Technique[技术] by (71.8m points)

iphone - Unrecognized Selector SenTestCase for Category

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...