我正在使用 RestKit ~> 0.20.3 和 RestKit/Testing ~> 0.20.3。这是我的映射的一个示例:
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[PlayerVO class]];
[mapping addAttributeMappingsFromArray:@[@"firstName", @"middeName", @"lastName", @"dob", @"sex"]];
这是我的模拟数据:
NSDictionary *data = @{@"players": @[@{@"firstName": @"Ahmed", @"middleName": @"Ahmed", @"lastName": @"Ahmed", @"dob": @"100", @"sex": @"m"}]};
这是我的mappingTest
:
RKMappingTest *mappingTest = [RKMappingTest testForMapping:mapping sourceObject:data destinationObject:nil];
最后我的expectation
:
RKPropertyMappingTestExpectation *expectation = [RKPropertyMappingTestExpectation expectationWithSourceKeyPath:@"players.firstName" destinationKeyPath:@"firstName" evaluationBlock:^BOOL(RKPropertyMappingTestExpectation *expectation, RKPropertyMapping *mapping, id mappedValue, NSError *__autoreleasing *error) {
BOOL expect = [mappedValue length] > 0;
XCTAssertTrue(expect);
return expect;
}];
[mappingTest addExpectation:expectation];
XCTAssertTrue([mappingTest evaluate]);
XCTAssertNoThrow([mappingTest verify]);
所以这个测试失败了,因为我似乎没有办法指定要遵循的关键路径data
是一个数组。这是我得到的错误:
testPlayerServiceGetPlayers] : (([mappingTest verify]) does not throw) failed: throwing "0x8e7d770: failed with error: (null)
RKMappingTest Expectations: (
"map 'players.firstName' to 'firstName' satisfying evaluation block"
)
Events: (
) during mapping from {
players = (
{
dob = 100;
firstName = Ahmed;
lastName = Ahmed;
middleName = Ahmed;
sex = m;
}
);
} to (null) with mapping <RKObjectMapping:0x8e6aad0 objectClass=PlayerVO propertyMappings=(
"<RKAttributeMapping: 0x8e6b880 firstName => firstName>",
"<RKAttributeMapping: 0x8e61df0 middeName => middeName>",
"<RKAttributeMapping: 0x8e2f580 lastName => lastName>",
"<RKAttributeMapping: 0x8e67a60 dob => dob>",
"<RKAttributeMapping: 0x8e2c120 sex => sex>"
)>"
如果我将数据更改为:
NSDictionary *data = @{@"firstName": @"Ahmed", @"middleName": @"Naseer", @"lastName": @"Nuaman", @"dob": @"524534400", @"sex": @"m"};
And the expectation
's expectationWithSourceKeyPath
to @"firstName"
,测试通过。因此,这让我相信这个问题显然与设定关键路径有关。现在在我的应用程序中使用:
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping method:RKRequestMethodAny pathPattern:nil keyPath:@"players" statusCodes:statusCodes];
但是我找不到设置的方法keyPath
for a RKMappingTest
or RKPropertyMappingTestExpectation
,关于如何实现这一目标有什么想法吗?
Update
所以我浏览了一下RKMappingTest.h并找到了rootKeyPath。我设置如下:
mappingTest.rootKeyPath = @"players";
并且仍然遇到问题mappedValue
in the RKPropertyMappingTestExpectation
。所以我也将我的数据更改为:
NSDictionary *data = @{@"players": @{@"firstName": @"Ahmed", @"middleName": @"Ahmed", @"lastName": @"Ahmed", @"dob": @"100", @"sex": @"m"}};
现在我可以看到mappedValue
现在设置为Ahmed
并更新了RKPropertyMappingTestExpectation
至以下内容:
RKPropertyMappingTestExpectation *expectation = [RKPropertyMappingTestExpectation expectationWithSourceKeyPath:@"firstName" destinationKeyPath:@"firstName" evaluationBlock:^BOOL(RKPropertyMappingTestExpectation *expectation, RKPropertyMapping *mapping, id mappedValue, NSError *__autoreleasing *error) {
return [mappedValue isEqualToString:@"Ahmed"];
}];
但这使用的是对象而不是数组。还有更多建议吗?