Ios RestKit测试映射与单独类中的RKObjectMapping不工作
我有一个模型类Ios RestKit测试映射与单独类中的RKObjectMapping不工作,ios,mapping,restkit,xctest,Ios,Mapping,Restkit,Xctest,我有一个模型类ApplicationError,如下所示: @interface ApplicationError : NSObject @property (nonatomic, strong) NSString *status; @property (nonatomic, strong) NSString *code; @property (nonatomic, strong) NSString *message; @end 在一个单独的类MappingProvider中,我创建了我的RK
ApplicationError
,如下所示:
@interface ApplicationError : NSObject
@property (nonatomic, strong) NSString *status;
@property (nonatomic, strong) NSString *code;
@property (nonatomic, strong) NSString *message;
@end
在一个单独的类MappingProvider
中,我创建了我的RKObjectMapping
,以适合此模型:
@implementation MappingProvider
+ (RKObjectMapping *)mappingForApplicationError {
RKObjectMapping *errorMapping = [RKObjectMapping mappingForClass:[ApplicationError class]];
[errorMapping addAttributeMappingsFromArray:[self propertiesError]];
return errorMapping;
}
+ (NSArray *)propertiesError {
return @[@"status", @"code", @"message"];
}
当测试这个映射时,我的测试并没有像应该的那样成功
- (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
是从Fixture JSON文件解析的id
-对象。
当我用这个方法(而不是在单独的MappingProvider
-类中)创建错误映射时,一切正常
RKObjectMapping *errorMapping = [RKObjectMapping mappingForClass:[ApplicationError class]];
[errorMapping addAttributeMappingsFromArray:@[@"status", @"code", @"message"]];
记录两个版本时,它们看起来相同(只是内存位置不同)。
知道为什么会这样吗
更新
日志显示:
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>"
)>"
AE_映射_1=
AE_映射_2=
(([mappingTest evaluate])为true)失败:抛出“0x15a98f60:失败,错误为:(null)
RKMappingTest期望值:(
“将“状态”映射到“状态”
)
活动:(
)在从{type=immutable dict,count=3进行映射期间,
条目=>
0:{contents=“status”}={value=+400,type=kCFNumberSInt64Type}
1:{contents=“code”}={value=+4002,type=kCFNumberSInt64Type}
2:{contents=“message”}={contents=“出了问题。”}
}
使用“映射”设置为(null)
错误:-[ApplicationErrorMappingTests testMappingOfStatus]:([mappingTest evaluate])失败:引发“0x15a98f60:失败,错误为:(null)
RKMappingTest期望值:(
“将“状态”映射到“状态”
)
活动:(
)在从{type=immutable dict,count=3进行映射期间,
条目=>
0:{contents=“status”}={value=+400,type=kCFNumberSInt64Type}
1:{contents=“code”}={value=+4002,type=kCFNumberSInt64Type}
2:{contents=“message”}={contents=“出了问题。”}
}
使用“映射”设置为(null)
我找到了解决方案:显然,您必须在测试目标中添加单独的类(在我的例子中是MappingProvider
-class)
仅将类添加到链接的开发目标中是不够的…而且JSON在两个测试中都是相同的?JSON是什么?代码看起来不错。JSON看起来是这样的:{“状态”:400,“代码”:4002,“消息”:“出了问题。”}奇怪的是,它在第二个选项中工作(使用测试方法中声明的映射),但我不希望复制/粘贴我的提供程序类,而只是使用它……在这两种情况下,发布的代码看起来都正常。如果一个测试中的JSON是一个字典数组,那么它应该会失败。除此之外,没有明显的问题……跟踪日志记录可能会用日志记录语句更新;-)