0

我正在使用 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 时,一切正常。

我会很感激任何帮助。

4

1 回答 1

0

尝试设置destinationObject为零:

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

于 2013-08-01T03:05:20.490 回答