Question

I'm attempting to follow along with the RestKit unit test guide ( https://github.com/RestKit/RestKit/wiki/Unit-Testing-with-RestKit ) except that my project is a static library instead of an app.

Here is the test I've written:

- (void)testMappingOfDate
{
    id parsedJSON = [RKTestFixture parsedObjectWithContentsOfFixture:@"plan.json"];
    RKMappingTest *test = [RKMappingTest testForMapping:[self planMapping] object:parsedJSON];
    [test expectMappingFromKeyPath:@"plan.date" toKeyPath:@"date"];
    STAssertNoThrow([test verify], nil);
}

When I attempt to run the test I receive this error on the first line of the test:

error: testMappingOfDate (api_clientTests) failed: -[NSBundle parsedObjectWithContentsOfResource:withExtension:]: unrecognized selector sent to instance 0x1765c40

It seems like its not finding the NSBundle category defined by RestKit, but my test target header search path is set to "$(BUILT_PRODUCTS_DIR)/../../Headers" and I've verified this path includes NSBundle+RKAdditions.h which contains the supposed "unrecognized selector".

Is there something I'm missing here?

Was it helpful?

Solution

You are trying to include a category within your binary that comes from a library. To get that accomplished you will need to add the following to your (Unit-Test-Target's) build settings.

Other Linker Flags: -ObjC

enter image description here

From Apple's QA:

Objective-C does not define linker symbols for each function (or method, in Objective-C) - instead, linker symbols are only generated for each class. If you extend a pre-existing class with categories, the linker does not know to associate the object code of the core class implementation and the category implementation. This prevents objects created in the resulting application from responding to a selector that is defined in the category.

Solution:

To resolve this issue, the static library should pass the -ObjC option to the linker. This flag causes the linker to load every object file in the library that defines an Objective-C class or category. While this option will typically result in a larger executable (due to additional object code loaded into the application), it will allow the successful creation of effective Objective-C static libraries that contain categories on existing classes.

OTHER TIPS

The error means that the "unrecognized selector" issue is at runtime. The compiler and NSBundle+RKAdditions.h do not give this error they would at compile timr.

The issue is that the code that has @implementation NSBundle(RKAdditions) is not linked into your app. So you need to add this to your build

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top