1
votes

As the title of this question indicates, I am using RestKit and I would like to test my object mapping using the build-in testing classes. I have written a tiny application that fetches a User object as json from a (local) web api. This part works but, my test case tells me that it doesn't - hm!

I am following Blake Watters Unit Testing With RestKit Guide, but I am not able to succeed. Let me clarify once more that the actual application works fine (I am able to map json from API to User object without problems), but the test case fails with the error shown below. Therefore I suspect my test to be wrong, somehow.

Here's some code:

Error Message

<RKMappingTest: 0xa242130>: failure when mapping from {
user =     {
    "first_name" = Kasper;
    id = 21;
    "last_name" = Fueled;
};
} to <User: 0xa23d730> with mapping <RKObjectMapping:0xa23d250 objectClass=User keyPath mappings => (
"RKObjectKeyPathMapping: id => userID",
"RKObjectKeyPathMapping: first_name => first_name",
"RKObjectKeyPathMapping: last_name => last_name",
)>. 

I notice that the id key is not quoted, while first_name and last_name are, but I am not sure if it makes any difference.

user.json (updated: removed comma)

{ "user": {
       "id":21,
       "first_name":"Kasper",
       "last_name":"Fueled"
       }
}

user.json (old)

{ "user": {
       "id":21,
       "first_name":"Kasper",
       "last_name":"Fueled",
       }
}

User.h

@property (strong, nonatomic) NSNumber *userID;
@property (strong, nonatomic) NSString *first_name;
@property (strong, nonatomic) NSString *last_name;

User mapping

+ (RKObjectMapping *)objectMapping {
    return [RKObjectMapping mappingForClass:[User class] usingBlock:^(RKObjectMapping *mapping) {
        [mapping mapKeyPath:@"id" toAttribute:@"userID"];
        [mapping mapKeyPath:@"first_name" toAttribute:@"first_name"];
        [mapping mapKeyPath:@"last_name" toAttribute:@"last_name"];
    }];
}

Test Case

- (void)testMapping {
    id parsedJSON = [RKTestFixture parsedObjectWithContentsOfFixture:@"user.json"];
    RKMappingTest *test = [RKMappingTest testForMapping:[User objectMapping] object:parsedJSON];

    [test expectMappingFromKeyPath:@"user.first_name" toKeyPath:@"first_name"];

    STAssertNoThrow([test verify], nil);
}

Fixture is created as per described in Blake's guide.

1

1 Answers

0
votes

Okay, as embarrassing as it is, I will not post the solution to my problem. I was right with my assumption that something was wrong with my test setup and not the code that I was testing. It turns out that there was a comma at the end of the last key-value-pair in my json fixture. I hand wrote the json fixture instead of copying it directly from the output of my API. The lesson learned must therefore be:

Copy / paste the json output for which you are trying to test object mapping into your fixture files. Don't just type it by hand - misspelling etc. might occur.

Hopefully I am the only one in the community who's doing (or did!) it that way.