Skip to content

Commit

Permalink
Fix bug in mapper delegate configuration and add missing test coverag…
Browse files Browse the repository at this point in the history
…e for response mapper operation and both object request operation classes. closes RestKit#1156
  • Loading branch information
blakewatters committed Jan 14, 2013
1 parent 999edf8 commit 840b37a
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 1 deletion.
1 change: 1 addition & 0 deletions Code/Network/RKResponseMapperOperation.m
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ - (RKMappingResult *)performMappingWithObject:(id)sourceObject error:(NSError **
RKObjectMappingOperationDataSource *dataSource = [RKObjectMappingOperationDataSource new];
self.mapperOperation = [[RKMapperOperation alloc] initWithRepresentation:sourceObject mappingsDictionary:self.responseMappingsDictionary];
self.mapperOperation.mappingOperationDataSource = dataSource;
self.mapperOperation.delegate = self.mapperDelegate;
if (NSLocationInRange(self.response.statusCode, RKStatusCodeRangeForClass(RKStatusCodeClassSuccessful))) {
self.mapperOperation.targetObject = self.targetObject;
} else {
Expand Down
30 changes: 30 additions & 0 deletions Tests/Logic/Network/RKManagedObjectRequestOperationTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@ - (NSSet *)localObjectsFromFetchRequestsMatchingRequestURL:(NSError **)error;
@end
NSSet *RKSetByRemovingSubkeypathsFromSet(NSSet *setOfKeyPaths);


@interface RKTestDelegateManagedObjectRequestOperation : RKManagedObjectRequestOperation
@end

@implementation RKTestDelegateManagedObjectRequestOperation

- (void)mapperWillStartMapping:(RKMapperOperation *)mapper
{
// For stubbing
}

@end

@interface RKManagedObjectRequestOperationTest : RKTestCase

@end
Expand Down Expand Up @@ -787,4 +800,21 @@ - (void)testThatSuccessfulCompletionSavesManagedObjectIfTargetObjectIsUnsavedEve
}];
}

- (void)testThatMapperOperationDelegateIsPassedThroughToUnderlyingMapperOperation
{
RKManagedObjectStore *managedObjectStore = [RKTestFactory managedObjectStore];
RKEntityMapping *entityMapping = [RKEntityMapping mappingForEntityForName:@"Human" inManagedObjectStore:managedObjectStore];
[entityMapping addAttributeMappingsFromArray:@[ @"name" ]];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:entityMapping pathPattern:nil keyPath:@"human" statusCodes:[NSIndexSet indexSetWithIndex:200]];

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/JSON/humans/with_to_one_relationship.json" relativeToURL:[RKTestFactory baseURL]]];
RKTestDelegateManagedObjectRequestOperation *managedObjectRequestOperation = [[RKTestDelegateManagedObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[ responseDescriptor ]];
id mockOperation = [OCMockObject partialMockForObject:managedObjectRequestOperation];
[[mockOperation expect] mapperWillStartMapping:OCMOCK_ANY];
managedObjectRequestOperation.managedObjectContext = managedObjectStore.persistentStoreManagedObjectContext;
[managedObjectRequestOperation start];
expect([managedObjectRequestOperation isFinished]).will.beTruthy();
[mockOperation verify];
}

@end
23 changes: 23 additions & 0 deletions Tests/Logic/Network/RKObjectRequestOperationTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ @interface RKTestComplexUser : NSObject

@end

@interface RKMapperTestObjectRequestOperation : RKObjectRequestOperation
@end

@implementation RKMapperTestObjectRequestOperation

- (void)mapperWillStartMapping:(RKMapperOperation *)mapper
{
// Used for stubbing
}

@end

@implementation RKTestComplexUser
@end

Expand Down Expand Up @@ -597,4 +609,15 @@ - (void)testThatLoadingAnUnexpectedStatusCodeReturnsCorrectErrorMessage
expect([requestOperation.error localizedDescription]).to.equal(@"Expected status code in (200-299,400-499), got 503");
}

- (void)testThatMapperOperationDelegateIsPassedThroughToUnderlyingMapperOperation
{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"/JSON/ComplexNestedUser.json" relativeToURL:[RKTestFactory baseURL]]];
RKMapperTestObjectRequestOperation *requestOperation = [[RKMapperTestObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[ [self responseDescriptorForComplexUser] ]];
id mockOperation = [OCMockObject partialMockForObject:requestOperation];
[[mockOperation expect] mapperWillStartMapping:OCMOCK_ANY];
[requestOperation start];
expect([requestOperation isFinished]).will.beTruthy();
[mockOperation verify];
}

@end
22 changes: 21 additions & 1 deletion Tests/Logic/Network/RKResponseMapperOperationTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ - (NSString *)description
@end

@interface RKObjectResponseMapperOperationTest : RKTestCase

@end

@implementation RKObjectResponseMapperOperationTest
Expand Down Expand Up @@ -293,4 +292,25 @@ - (void)testThatObjectResponseMapperOperationDoesNotMapWithTargetObjectForUnsucc
expect([mapper.error code]).notTo.equal(RKMappingErrorTypeMismatch);
}

- (void)testThatMapperOperationDelegateIsPassedThroughToUnderlyingMapperOperation
{
id mockDelegate = [OCMockObject niceMockForProtocol:@protocol(RKMapperOperationDelegate)];
[[mockDelegate expect] mapperWillStartMapping:OCMOCK_ANY];

NSURL *responseURL = [NSURL URLWithString:@"http://restkit.org/api/v1/users"];
NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] initWithURL:responseURL statusCode:200 HTTPVersion:@"1.1" headerFields:@{@"Content-Type": @"application/json"}];
NSData *data = [@"{\"name\": \"Blake\"}" dataUsingEncoding:NSUTF8StringEncoding];

RKTestUser *testUser = [RKTestUser new];
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[RKTestUser class]];
[mapping addAttributeMappingsFromArray:@[ @"name" ]];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping pathPattern:nil keyPath:nil statusCodes:[NSIndexSet indexSetWithIndex:200]];

RKObjectResponseMapperOperation *mapper = [[RKObjectResponseMapperOperation alloc] initWithResponse:response data:data responseDescriptors:@[ responseDescriptor ]];
mapper.mapperDelegate = mockDelegate;
mapper.targetObject = testUser;
[mapper start];
[mockDelegate verify];
}

@end

0 comments on commit 840b37a

Please sign in to comment.