0

I am using Restkit 0.20.3 and trying to setup a simple test scenario:

I have a simple model that contains a static method to return its mapping:

@interface TKTag : NSObject

@property (nonatomic) int _id;
@property (strong, nonatomic) NSString *name;

+ (RKObjectMapping *)objectMapping;

@end

@implementation TKTag

+ (RKObjectMapping *)objectMapping
{
    RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[TKTag class]];
    [mapping addAttributeMappingsFromDictionary:@{
        @"TAG_ID": @"_id",
        @"TAG": @"name"
    }];
    return mapping;
}
@end

And here is my test:

- (void)testObjectMapping
{
    NSBundle *testTargetBundle = [NSBundle bundleWithIdentifier:@"here.is.my.bundeidTests"];
[RKTestFixture setFixtureBundle:testTargetBundle];

    TKTag *tag = [TKTag new];
    id parsedJSON = [RKTestFixture parsedObjectWithContentsOfFixture:@"tag.json"];
RKMappingTest *test = [RKMappingTest testForMapping:[TKTag objectMapping] sourceObject:parsedJSON destinationObject:tag];

    [test addExpectation:[RKPropertyMappingTestExpectation expectationWithSourceKeyPath:@"TAG_ID" destinationKeyPath:@"_id"]];
[test addExpectation:[RKPropertyMappingTestExpectation expectationWithSourceKeyPath:@"TAG" destinationKeyPath:@"name"]];

    STAssertTrue([test evaluate], nil);
}

When I run the tests, I just receive a failure on [test evaluate] with the message -[TKTagTest testObjectMapping] failed 0xacb67e0: failed with error: (null)

Here is my json:

{
    "TAG_ID": "30308",
    "TAG": "mendesss",
    "POST_ID": "68213"
}

Debugging the execution and setting the the Restkit log level to Debug using RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace), I found:

Test Case '-[TKTagTest testObjectMapping]' started.
2013-07-21 22:45:01.437 Teckler[5332:a0b] D restkit.object_mapping:RKMappingOperation.m:952 Starting mapping     operation...
2013-07-21 22:45:01.438 Teckler[5332:a0b] T restkit.object_mapping:RKMappingOperation.m:953 Performing     mapping operation: <RKMappingOperation 0x105b3690> for 'TKTag' object. Mapping values from object {
    "POST_ID" = 68213;
    TAG = mendesss;
    "TAG_ID" = 30308;
} to object <TKTag: 0xacedb90> with object mapping (null)
2013-07-21 22:45:01.438 Teckler[5332:a0b] D restkit.object_mapping:RKMappingOperation.m:598 Key-value     validation is disabled for mapping, skipping...
2013-07-21 22:45:01.438 Teckler[5332:a0b] D restkit.object_mapping:RKMappingOperation.m:598 Key-value     validation is disabled for mapping, skipping...
2013-07-21 22:45:01.439 Teckler[5332:a0b] D restkit.object_mapping:RKMappingOperation.m:997 Mapping operation     did not find any mappable values for the attribute and relationship mappings in the given object     representation
2013-07-21 22:45:01.439 Teckler[5332:a0b] D restkit.object_mapping:RKMappingOperation.m:1019 Failed mapping     operation: No mappable values found for any of the attributes or relationship mappings
Unknown.m:0: error: -[TKTagTest testObjectMapping] : 0xac56880: failed with error: (null)
RKMappingTest Expectations: (
    "map 'TAG_ID' to '_id'",
    "map 'TAG' to 'name'"
)
Events: (
) during mapping from <CFBasicHash 0xac57e90 [0x2dccd88]>{type = immutable dict, count = 3,
entries =>
    0 : <CFString 0xac460e0 [0x2dccd88]>{contents = "TAG_ID"} = <CFString 0xac45f70 [0x2dccd88]>{contents =     "30308"}
    1 : <CFString 0xac45f80 [0x2dccd88]>{contents = "TAG"} = <CFString 0xac45ec0 [0x2dccd88]>{contents =     "mendesss"}
    2 : <CFString 0xac45e10 [0x2dccd88]>{contents = "POST_ID"} = <CFString 0xac45d60 [0x2dccd88]>{contents =     "68213"}
}
 to <TKTag: 0xacedb90> with mapping <RKObjectMapping:0xac45bd0 objectClass=TKTag propertyMappings=(
    "<RKAttributeMapping: 0xac45ca0 TAG_ID => _id>",
    "<RKAttributeMapping: 0xac45990 TAG => name>"
)>
Test Case '-[TKTagTest testObjectMapping]' failed (5.937 seconds).
Test Suite 'TKTagTest' finished at 2013-07-22 01:45:01 +0000.

Any ideas about what I am doing wrong?

This issue is only when I am running the unit tests. Everything is ok when I am performing a request to the web server and mapping the JSON.

I'll appreciate any help.

Gustavo Barbosa
  • 1,340
  • 1
  • 17
  • 27

1 Answers1

0

Try setting destinationObject to nil:

RKMappingTest *test = [RKMappingTest testForMapping:[DTOMapping myMapping] sourceObject:parsedJSON destinationObject:nil];

Craig Bruce
  • 674
  • 1
  • 8
  • 18
  • Apparently, I can't do that. New ST: "Cannot perform a mapping operation with a nil destination object." – Gustavo Barbosa Aug 01 '13 at 20:48
  • @GustavoBarbosa you might want to check out [this](https://github.com/RestKit/RestKit/issues/1904) and the other issue linked on it. If you're static method for returning that mapping is in your main app, then you may be getting the "Cannot perform a mapping..." error incorrectly. I've posted a workaround on that bug report. – Sandy Chapman May 02 '14 at 19:05