I found the solution: apparently, you have to add the separate class (in my case the MappingProvider
-class) in your test-target.
It is not enough to add the class to your linked dev-target...
سؤال
I have a Model-class ApplicationError
which looks like this:
@interface ApplicationError : NSObject
@property (nonatomic, strong) NSString *status;
@property (nonatomic, strong) NSString *code;
@property (nonatomic, strong) NSString *message;
@end
In a separate class MappingProvider
, I create my RKObjectMapping
to fit this model:
@implementation MappingProvider
+ (RKObjectMapping *)mappingForApplicationError {
RKObjectMapping *errorMapping = [RKObjectMapping mappingForClass:[ApplicationError class]];
[errorMapping addAttributeMappingsFromArray:[self propertiesError]];
return errorMapping;
}
+ (NSArray *)propertiesError {
return @[@"status", @"code", @"message"];
}
When testing this mapping, my test does not succeed like it should.
- (void)testMappingOfStatus {
RKObjectMapping *errorMapping = [MappingProvider mappingForApplicationError];
RKMappingTest *mappingTest = [RKMappingTest testForMapping:errorMapping sourceObject:self.parsedJSON destinationObject:nil];
[mappingTest addExpectation:[RKPropertyMappingTestExpectation expectationWithSourceKeyPath:@"status" destinationKeyPath:@"status"]];
XCTAssert([mappingTest evaluate]);
}
self.parsedJSON
is an id
-object parsed from a Fixture-JSON-file.
When I create my errorMapping in this method (instead of in the separate MappingProvider
-class, everything works fine.
RKObjectMapping *errorMapping = [RKObjectMapping mappingForClass:[ApplicationError class]];
[errorMapping addAttributeMappingsFromArray:@[@"status", @"code", @"message"]];
When logging both versions, they look the same (only the memory-location is different). Any ideas why this is happening?
UPDATE
Logging shows:
AE_MAPPING_1 = <RKObjectMapping:0x1501a750 objectClass=ApplicationError propertyMappings=(
"<RKAttributeMapping: 0x1505e0b0 status => status>",
"<RKAttributeMapping: 0x1505e0c0 code => code>",
"<RKAttributeMapping: 0x15a94850 message => message>"
)>
AE_MAPPING_2 = <RKObjectMapping:0x1505a660 objectClass=ApplicationError propertyMappings=(
"<RKAttributeMapping: 0x1505b620 status => status>",
"<RKAttributeMapping: 0x1505b630 code => code>",
"<RKAttributeMapping: 0x1505b640 message => message>"
)>
(([mappingTest evaluate]) is true) failed: throwing "0x15a98f60: failed with error: (null)
RKMappingTest Expectations: (
"map 'status' to 'status'"
)
Events: (
) during mapping from <CFBasicHash 0x15031030 [0x31d8ec8]>{type = immutable dict, count = 3,
entries =>
0 : <CFString 0x15a957b0 [0x31d8ec8]>{contents = "status"} = <CFNumber 0x15a95240 [0x31d8ec8]>{value = +400, type = kCFNumberSInt64Type}
1 : <CFString 0x15a95250 [0x31d8ec8]>{contents = "code"} = <CFNumber 0x15a95c00 [0x31d8ec8]>{value = +4002, type = kCFNumberSInt64Type}
2 : <CFString 0x15a96130 [0x31d8ec8]>{contents = "message"} = <CFString 0x15a97310 [0x31d8ec8]>{contents = "Something went wrong."}
}
to (null) with mapping <RKObjectMapping:0x1505a660 objectClass=ApplicationError propertyMappings=(
"<RKAttributeMapping: 0x1505b620 status => status>",
"<RKAttributeMapping: 0x1505b630 code => code>",
"<RKAttributeMapping: 0x1505b640 message => message>"
)>"
error: -[ApplicationErrorMappingTests testMappingOfStatus] : (([mappingTest evaluate]) is true) failed: throwing "0x15a98f60: failed with error: (null)
RKMappingTest Expectations: (
"map 'status' to 'status'"
)
Events: (
) during mapping from <CFBasicHash 0x15031030 [0x31d8ec8]>{type = immutable dict, count = 3,
entries =>
0 : <CFString 0x15a957b0 [0x31d8ec8]>{contents = "status"} = <CFNumber 0x15a95240 [0x31d8ec8]>{value = +400, type = kCFNumberSInt64Type}
1 : <CFString 0x15a95250 [0x31d8ec8]>{contents = "code"} = <CFNumber 0x15a95c00 [0x31d8ec8]>{value = +4002, type = kCFNumberSInt64Type}
2 : <CFString 0x15a96130 [0x31d8ec8]>{contents = "message"} = <CFString 0x15a97310 [0x31d8ec8]>{contents = "Something went wrong."}
}
to (null) with mapping <RKObjectMapping:0x1505a660 objectClass=ApplicationError propertyMappings=(
"<RKAttributeMapping: 0x1505b620 status => status>",
"<RKAttributeMapping: 0x1505b630 code => code>",
"<RKAttributeMapping: 0x1505b640 message => message>"
)>"
المحلول
I found the solution: apparently, you have to add the separate class (in my case the MappingProvider
-class) in your test-target.
It is not enough to add the class to your linked dev-target...