RestKit测试映射问题

时间:2013-07-22 01:59:49

标签: ios objective-c restkit restkit-0.20 sentestingkit

我正在使用Restkit 0.20.3并尝试设置一个简单的测试场景:

我有一个简单的模型,它包含一个返回其映射的静态方法:

@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

这是我的测试:

- (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);
}

当我运行测试时,我只是在[test evaluate]收到有关消息的失败 -[TKTagTest testObjectMapping] failed 0xacb67e0: failed with error: (null)

这是我的json:

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

调试执行并使用RKLogConfigureByName(“RestKit / ObjectMapping”,RKLogLevelTrace)将Restkit日志级别设置为Debug,我发现:

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.

关于我做错了什么的想法?

此问题仅在我运行单元测试时。当我向Web服务器执行请求并映射JSON时,一切正常。

我会感激任何帮助。

1 个答案:

答案 0 :(得分:0)

尝试将destinationObject设为nil:

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