问题描述
我正在使用 RestKit ~> 0.20.3 和 RestKit/Testing ~> 0.20.3.所以这是我的映射示例:
I'm using RestKit ~> 0.20.3 and RestKit/Testing ~> 0.20.3. So this is an example of my mapping:
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];
最后是我的期望
:
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
是一个数组,所以我似乎没有办法指定要遵循的关键路径.这是我得到的错误:
So this test fails as there doesn't seem to be a way I can specify a key path to follow as the data
is an array. This is the error I get:
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"};
和expectation
的expectationWithSourceKeyPath
到@"firstName"
,测试通过.所以这让我相信这个问题显然与设置关键路径有关.现在在我的应用程序中使用:
And the expectation
's expectationWithSourceKeyPath
to @"firstName"
, the tests pass. So this leads me to believe the issue is clearly relative to setting the key path. Now in my app that's done using:
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping method:RKRequestMethodAny pathPattern:nil keyPath:@"players" statusCodes:statusCodes];
但是,我找不到为 RKMappingTest
或 RKPropertyMappingTestExpectation
设置 keyPath
的方法,关于如何实现这一点的任何想法?
However I can't find a way of setting the keyPath
for a RKMappingTest
or RKPropertyMappingTestExpectation
, any ideas on how I can achieve this?
更新
所以我查看了RKMappingTest.h
并找到了rootKeyPath
.我设置了以下内容:
So I had a look throughRKMappingTest.h
and found the rootKeyPath
. I set the following:
mappingTest.rootKeyPath = @"players";
并且仍然有关于 RKPropertyMappingTestExpectation
中的 mappedValue
的问题.所以我也把我的数据改成了这样:
And still was having problems regarding the mappedValue
in the RKPropertyMappingTestExpectation
. So I also changed my data to this:
NSDictionary *data = @{@"players": @{@"firstName": @"Ahmed", @"middleName": @"Ahmed", @"lastName": @"Ahmed", @"dob": @"100", @"sex": @"m"}};
现在我可以看到 mappedValue
现在设置为 Ahmed
并将 RKPropertyMappingTestExpectation
更新为以下内容:
And now I can see that mappedValue
is now set to Ahmed
and updated the RKPropertyMappingTestExpectation
to the following:
RKPropertyMappingTestExpectation *expectation = [RKPropertyMappingTestExpectation expectationWithSourceKeyPath:@"firstName" destinationKeyPath:@"firstName" evaluationBlock:^BOOL(RKPropertyMappingTestExpectation *expectation, RKPropertyMapping *mapping, id mappedValue, NSError *__autoreleasing *error) {
return [mappedValue isEqualToString:@"Ahmed"];
}];
但这是使用对象而不是数组.还有什么建议吗?
But this is using an object rather than an array. Any more suggestions?
推荐答案
您错误地考虑了测试,更具体地说是测试的范围.这是一个单元测试,单元是映射.映射处理单个项目,这就是修改后的测试有效的原因 - 因为范围是正确的.
You're thinking about testing, and more specifically the scope of testing, wrongly. This is a unit test, and the unit is the mapping. The mapping deals with individual items and that is why your modified test works - because the scope is correct.
响应描述符有不同的范围.您无法在映射测试中测试响应描述符的范围.
Response descriptors have a different scope. You can't test the scope of a response descriptor in a mapping test.
这篇关于RestKit RKMappingTest JSON 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!