1
votes

I followed the example from github and try testing my own RKModels:

RestKit Unit Testing

After starting my unit testing on Restkit using fixtures, I cannot find pass the test case which they test the mapping of nested relationship data.


My Fixture - 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"
                      ]
             }
             ]
}

My Test Case:

- (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]);
}

The fixture is loaded, however it received an error:

(([test evaluate]) is true) failed: throwing "0x112701a20: failed with error: Error Domain=org.restkit.RestKit.ErrorDomain Code=1003 "No mappable values found for any of the attributes or relationship mappings" UserInfo=0x112703bb0 {NSLocalizedDescription=No mappable values found for any of the attributes or relationship mappings}

It seems it cannot find the nested value because the responseDescriptor is not set yet. How can I solve these testing of nested relationship data?

Great Thanks!

1

1 Answers

2
votes

RKMappingTest is for testing individual mappings, not collections of mappings. So, a single dictionary, not an array of dictionaries. If you want to test a collection of mappings (and the associated key path) then you need to change your approach to also test the response descriptor.

You either need to change what you're trying to test or how you're trying to test it.