Restkit如何通过Fixture测试嵌套关系数据?

时间:2014-08-14 11:32:31

标签: objective-c unit-testing restkit restkit-0.20

我按照github的例子来测试我自己的RKModel:

RestKit Unit Testing

使用fixtures在Restkit上开始我的单元测试后,我找不到通过他们测试嵌套关系数据映射的测试用例。


我的夹具 - articles.json:

{
"articles": [
             {
             "title": "RestKit Unit Testing",
             "author": "Blake Watters",
             "tags": [
                      "RestKit",
                      "testing",
                      "tutorials"
                      ]
             },
             {
             "title": "RestKit Unit Testing2",
             "author": "Blake Watters",
             "tags": [
                      "RestKit",
                      "testing",
                      "tutorials"
                      ]
             },
             {
             "title": "RestKit Unit Testing3",
             "author": "Blake Watters",
             "tags": [
                      "RestKit",
                      "testing",
                      "tutorials"
                      ]
             }
             ]
}

我的测试用例:

- (void)testArticlesMapping
{
    id parsedJSON = [RKTestFixture parsedObjectWithContentsOfFixture:@"articles.json"];


    RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[RKArticle class]];
    [mapping addAttributeMappingsFromDictionary:@{
                                                  @"title":   @"title",
                                                  @"author":  @"author",
                                                  @"tags":    @"tags"
                                                  }];

    RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping method:RKRequestMethodAny pathPattern:nil keyPath:@"articles" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

    [[RKObjectManager sharedManager] addResponseDescriptor:responseDescriptor];

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

    [test addExpectation:[RKPropertyMappingTestExpectation expectationWithSourceKeyPath:@"title" destinationKeyPath:@"title" value:@"RestKit Unit Testing"]];
    [test addExpectation:[RKPropertyMappingTestExpectation expectationWithSourceKeyPath:@"author" destinationKeyPath:@"author" value:@"Blake Watters"]];
    [test addExpectation:[RKPropertyMappingTestExpectation expectationWithSourceKeyPath:@"tags" destinationKeyPath:@"tags" value:@[ @"RestKit", @"testing", @"tutorials"]]];


    XCTAssertTrue([test evaluate]);
    XCTAssertNoThrow([test verify]);
}

夹具已加载,但收到错误:

  

(([test evaluate])为真)失败:抛出“0x112701a20:失败了   错误:错误Domain = org.restkit.RestKit.ErrorDomain Code = 1003“No   找到任何属性或关系的可映射值   映射“UserInfo = 0x112703bb0 {NSLocalizedDescription = No mappable   为任何属性或关系映射找到的值}

似乎无法找到嵌套值,因为尚未设置responseDescriptor。 如何解决嵌套关系数据的这些测试?

非常感谢!

1 个答案:

答案 0 :(得分:2)

RKMappingTest用于测试单个映射,而不是映射集合。所以,一个字典,而不是一个字典数组。如果要测试映射集合(以及关联的密钥路径),则需要更改方法以测试响应描述符。

您需要更改您尝试测试的内容或您尝试测试的内容。