2013-07-22 101 views
0

我使用Restkit 0.20.3,並試圖建立一個簡單的測試場景:RestKit測試映射問題

我有一個包含靜態方法返回其映射一個簡單的模型:

@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日誌級別來調試,我發現:

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時,一切正常。

我會很感激任何幫助。

回答

0

嘗試設置destinationObject爲nil:

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

+0

顯然,我不能這樣做。新ST:「無法執行映射操作與零目標對象。」 –

+0

@GustavoBarbosa你可能想查看[this](https://github.com/RestKit/RestKit/issues/1904)和其他相關問題。如果您是用於返回該映射的靜態方法,那麼您可能會錯誤地獲取「無法執行映射...」錯誤。我已經在該錯誤報告上發佈了一個解決方法。 –