【发布时间】:2013-12-01 23:33:13
【问题描述】:
我想测试我的项目模型的对象映射 (RKObjectMapping)。我在项目中创建了 .json 文件。它包含:
{"access_token" : "this_is_a_token_123"}
当它在请求后被映射时,它在 mappingResult 对象中看起来像这样:
2013-12-02 07:15:00.315 Capture The Flag[30320:70b] mapp = <RKMappingResult: 0x8e89ba0, results= {
"<null>" = "<CTFAPIOBJToken: 0x8e8b690>";
}>
类接口:
@interface CTFAPIOBJToken : NSObject
@property NSString *value;
+ (RKResponseDescriptor *)responseDescriptor;
+ (RKObjectMapping *)objectMapping;
@end
类实现:
@implementation CTFAPIOBJToken
+ (RKObjectMapping *)objectMapping {
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[CTFAPIOBJToken class]];
[mapping addAttributeMappingsFromDictionary:@{@"access_token" : @"value"}];
return mapping;
}
并测试:
- (void)setUp {
[super setUp];
NSBundle *bundle = [NSBundle bundleWithIdentifier:[[NSBundle mainBundle].bundleIdentifier stringByAppendingString:@"Tests"]];
[RKTestFixture setFixtureBundle:bundle];
}
- (RKMappingTest *)mappingTest {
id parsedJSON = [RKTestFixture parsedObjectWithContentsOfFixture:@"token-response.json"];
RKMappingTest *test = [RKMappingTest testForMapping:[CTFAPIOBJToken objectMapping] sourceObject:parsedJSON destinationObject:nil];
return test;
}
- (void)testMapping_AccessToken {
RKMappingTest *test = [self mappingTest];
RKPropertyMappingTestExpectation *expectation =
[RKPropertyMappingTestExpectation expectationWithSourceKeyPath:@"access_token" destinationKeyPath:@"value"];
[test addExpectation:expectation];
NSError *error = nil;
BOOL result = [test evaluate];
NSLog(@"error = %@", error);
XCTAssertTrue(result, @"value should be defined");
}
现在,当我向服务器发出请求时,我成功接收到测试中的类对象,并且属性value 是来自access_token = this_is_a_token_123 的值作为响应从服务器接收。当我想测试它时,出现以下错误:
Test Case '-[CTFAPIOBJTokenTests testMapping_AccessToken]' started.
<unknown>:0: error: -[CTFAPIOBJTokenTests testMapping_AccessToken] : 0x8e4e680: failed with error: (null)
RKMappingTest Expectations: (
"map 'access_token' to 'value'"
)
Events: (
) during mapping from <CFBasicHash 0x8e4a360 [0x225dec8]>{type = immutable dict, count = 1,
entries =>
1 : <CFString 0x8e56fd0 [0x225dec8]>{contents = "access_token"} = <CFString 0x8e56ff0 [0x225dec8]>{contents = "this_is_a_token_123"}
}
to (null) with mapping <RKObjectMapping:0x8e4a390 objectClass=CTFAPIOBJToken propertyMappings=(
"<RKAttributeMapping: 0x8e438d0 access_token => value>"
)>
(
0 CoreFoundation 0x021125e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x01e958b6 objc_exception_throw + 44
2 CoreFoundation 0x021123bb +[NSException raise:format:] + 139
3 Capture The FlagTests 0x0a8e8a9b -[RKMappingTest performMapping] + 1067
4 Capture The FlagTests 0x0a8e93a3 -[RKMappingTest evaluate] + 99
5 Capture The FlagTests 0x0a80e612 -[CTFAPIOBJTokenTests testMapping_AccessToken] + 194
6 CoreFoundation 0x02106d1d __invoking___ + 29
7 CoreFoundation 0x02106c2a -[NSInvocation invoke] + 362
8 XCTest 0x201032bf -[XCTestCase invokeTest] + 212
9 XCTest 0x2010338d -[XCTestCase performTest:] + 111
10 XCTest 0x2010417c -[XCTest run] + 82
11 XCTest 0x20102a44 -[XCTestSuite performTest:] + 139
12 XCTest 0x2010417c -[XCTest run] + 82
13 XCTest 0x20102a44 -[XCTestSuite performTest:] + 139
14 XCTest 0x2010417c -[XCTest run] + 82
15 XCTest 0x20102a44 -[XCTestSuite performTest:] + 139
16 XCTest 0x2010417c -[XCTest run] + 82
17 XCTest 0x20105aa1 +[XCTestProbe runTests:] + 183
18 Foundation 0x01acc12c __NSFireDelayedPerform + 372
19 CoreFoundation 0x020d0bd6 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 22
20 CoreFoundation 0x020d05bd __CFRunLoopDoTimer + 1181
21 CoreFoundation 0x020b8628 __CFRunLoopRun + 1816
22 CoreFoundation 0x020b7ac3 CFRunLoopRunSpecific + 467
23 CoreFoundation 0x020b78db CFRunLoopRunInMode + 123
24 GraphicsServices 0x03ad89e2 GSEventRunModal + 192
25 GraphicsServices 0x03ad8809 GSEventRun + 104
26 UIKit 0x00c03d3b UIApplicationMain + 1225
27 Capture The Flag 0x0000705d main + 141
28 libdyld.dylib 0x0275070d start + 1
)
Test Case '-[CTFAPIOBJTokenTests testMapping_AccessToken]' failed (0.028 seconds).
这个测试有什么问题?
编辑。
现在我认为这个对象映射是不正确的。应该是这样的:
{"tokenobject": {"access_token": "this is a token"}}
在我上面的例子中,this is a token 字符串似乎已映射到 CTFAPIOBJToken 对象的属性value,但它看起来很奇怪,因为映射结果中的整个对象都在键下:“null”,即NSNull或类似的东西吗?这很奇怪,测试也很奇怪。 IMO 在成功块中的 mappingResult.array 对象中应该如下所示:
{
CTFAPIOBJToken <address>
}
不使用映射的一种方法是使用AFNetworking,它也在 RestKit 中使用。 RKObjectManager 具有 client 或类似 AFHTTPClient 类型的属性。
【问题讨论】:
-
那么问题出在哪里?例外?或者这是固定的,问题是理解映射结果?您确认 JSON 了吗?
-
我不明白为什么我的映射对于主题顶部的 JSON 是错误的。以及为什么 mappingResult 具有对象
CTFAPIOBJToken的键为“null”的字典。
标签: ios objective-c unit-testing restkit