RestKit测试映射与RKObjectMapping在单独的类中不起作用

时间:2014-04-25 09:22:48

标签: 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中,我创建了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是一个id - 从Fixture-JSON文件解析的对象。 当我在这个方法中创建errorMapping时(而不是在单独的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>"
)>"

1 个答案:

答案 0 :(得分:0)

我找到了解决方案:显然,你必须在测试目标中添加单独的类(在我的例子中是MappingProvider - 类)。

仅将类添加到链接的dev-target ...

是不够的