Skip to content

Commit

Permalink
Add RKTestHelpers class containing helpful methods when working with …
Browse files Browse the repository at this point in the history
…object managers in unit and integration tests
  • Loading branch information
blakewatters committed Oct 3, 2012
1 parent e1bf7e2 commit a10df02
Show file tree
Hide file tree
Showing 6 changed files with 244 additions and 32 deletions.
1 change: 1 addition & 0 deletions Code/Testing.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
#import "RKTestFixture.h"
#import "RKTestNotificationObserver.h"
#import "RKTestFactory.h"
#import "RKTestHelpers.h"
#import "RKMappingTest.h"
25 changes: 12 additions & 13 deletions Code/Testing/RKTestFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@
// Created by Blake Watters on 2/16/12.
// Copyright (c) 2009-2012 RestKit. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#import <CoreData/CoreData.h>

Expand Down Expand Up @@ -211,17 +223,4 @@ extern NSString * const RKTestFactoryDefaultNamesManagedObjectStore;
*/
+ (void)tearDown;

///------------------
/// @name Other Tasks
///------------------

/**
Clears the contents of the cache directory by removing the directory and recreating it.
This has the effect of clearing any `NSCachedURLResponse` objects stored by `NSURLCache` as well as any application specific cache data.
@see `RKCachesDirectory()`
*/
+ (void)clearCacheDirectory;

@end
31 changes: 12 additions & 19 deletions Code/Testing/RKTestFactory.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@
// Created by Blake Watters on 2/16/12.
// Copyright (c) 2009-2012 RestKit. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#import "AFHTTPClient.h"
#import "RKTestFactory.h"
Expand Down Expand Up @@ -260,23 +272,4 @@ + (void)tearDown
}
}

+ (void)clearCacheDirectory
{
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];

NSError *error = nil;
NSString *cachePath = RKCachesDirectory();
BOOL success = [[NSFileManager defaultManager] removeItemAtPath:cachePath error:&error];
if (success) {
RKLogDebug(@"Cleared cache directory...");
success = [[NSFileManager defaultManager] createDirectoryAtPath:cachePath withIntermediateDirectories:YES attributes:nil error:&error];
if (!success) {
RKLogError(@"Failed creation of cache path '%@': %@", cachePath, [error localizedDescription]);
}
} else {
RKLogError(@"Failed to clear cache path '%@': %@", cachePath, [error localizedDescription]);
}
}

@end
99 changes: 99 additions & 0 deletions Code/Testing/RKTestHelpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
//
// RKTestHelpers.h
// RestKit
//
// Created by Blake Watters on 10/2/12.
// Copyright (c) 2012 RestKit. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#import <Foundation/Foundation.h>
#import "RKHTTPUtilities.h"

@class RKRoute, RKObjectManager;

/**
The `RKTestHelpers` class provides a number of helpful utility methods for use in unit or integration tests for RestKit applications.
*/
@interface RKTestHelpers : NSObject

///----------------------
/// @name Stubbing Routes
///----------------------

/**
Stubs the route with the given class and method with a given path pattern.
@param objectClass The class of the route to stub.
@param method The method of the route to stub.
@param pathPattern The path pattern to return instead in place of the current route's value.
@param nilOrObjectManager The object manager to stub the route on. If `nil`, the shared object manager be be used.
@return The new stubbed route object that was added to the route set of the target object manager.
*/
+ (RKRoute *)stubRouteForClass:(Class)objectClass
method:(RKRequestMethod)method
withPathPattern:(NSString *)pathPattern
onObjectManager:(RKObjectManager *)nilOrObjectManager;

/**
Stubs the route with the given name with a given path pattern.
@param routeName The name of the route to stub.
@param pathPattern The path pattern to return instead in place of the current route's value.
@param nilOrObjectManager The object manager to stub the route on. If `nil`, the shared object manager be be used.
@return The new stubbed route object that was added to the route set of the target object manager.
*/
+ (RKRoute *)stubRouteNamed:(NSString *)routeName
withPathPattern:(NSString *)pathPattern
onObjectManager:(RKObjectManager *)nilOrObjectManager;

/**
Stubs the relationship route for a given class with a given path pattern.
@param relationshipName The name of the relationship to stub the route of.
@param objectClass The class of the route to stub.
@param pathPattern The path pattern to return instead in place of the current route's value.
@param nilOrObjectManager The object manager to stub the route on. If `nil`, the shared object manager be be used.
@return The new stubbed route object that was added to the route set of the target object manager.
*/
+ (RKRoute *)stubRouteForRelationship:(NSString *)relationshipName
ofClass:(Class)objectClass
pathPattern:(NSString *)pathPattern
onObjectManager:(RKObjectManager *)nilOrObjectManager;

/**
Finds all registered fetch request blocks matching the given path pattern and adds a new fetch request block that returns the same value as the origin block that matches the given relative string portion of a URL object.
@param pathPattern The path pattern that matches the fetch request blocks to be copied.
@param relativeString The relative string portion of the NSURL objects that the new blocks will match exactly.
@param nilOrObjectManager The object manager to stub the route on. If `nil`, the shared object manager be be used.
*/
+ (void)copyFetchRequestBlocksMatchingPathPattern:(NSString *)pathPattern
toBlocksMatchingRelativeString:(NSString *)relativeString
onObjectManager:(RKObjectManager *)nilOrObjectManager;

///-------------------------------
/// @name Clearing the NSURL Cache
///-------------------------------

/**
Clears the contents of the cache directory by removing the directory and recreating it.
This has the effect of clearing any `NSCachedURLResponse` objects stored by `NSURLCache` as well as any application specific cache data.
@see `RKCachesDirectory()`
*/
+ (void)clearCacheDirectory;

@end
108 changes: 108 additions & 0 deletions Code/Testing/RKTestHelpers.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
//
// RKTestHelpers.m
// RestKit
//
// Created by Blake Watters on 10/2/12.
// Copyright (c) 2012 RestKit. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#import "RKTestHelpers.h"
#import "RKObjectManager.h"
#import "RKRoute.h"
#import "RKPathUtilities.h"
#import "RKLog.h"

@implementation RKTestHelpers

+ (RKRoute *)stubRouteForClass:(Class)objectClass method:(RKRequestMethod)method withPathPattern:(NSString *)pathPattern onObjectManager:(RKObjectManager *)nilOrObjectManager
{
RKObjectManager *objectManager = nilOrObjectManager ?: [RKObjectManager sharedManager];
RKRoute *route = [objectManager.router.routeSet routeForClass:objectClass method:method];
NSAssert(route, @"Expected to retrieve a route, but got nil");
[objectManager.router.routeSet removeRoute:route];
RKRoute *stubbedRoute = [RKRoute routeWithClass:objectClass pathPattern:pathPattern method:method];
[objectManager.router.routeSet addRoute:stubbedRoute];
return stubbedRoute;
}

+ (RKRoute *)stubRouteNamed:(NSString *)routeName withPathPattern:(NSString *)pathPattern onObjectManager:(RKObjectManager *)nilOrObjectManager
{
RKObjectManager *objectManager = nilOrObjectManager ?: [RKObjectManager sharedManager];
RKRoute *route = [[RKObjectManager sharedManager].router.routeSet routeForName:routeName];
NSAssert(route, @"Expected to retrieve a route, but got nil");
[[RKObjectManager sharedManager].router.routeSet removeRoute:route];
RKRoute *stubbedRoute = [RKRoute routeWithName:routeName pathPattern:pathPattern method:route.method];
[[RKObjectManager sharedManager].router.routeSet addRoute:stubbedRoute];
[self copyFetchRequestBlocksMatchingPathPattern:route.pathPattern toBlocksMatchingRelativeString:pathPattern onObjectManager:objectManager];
return stubbedRoute;
}

+ (RKRoute *)stubRouteForRelationship:(NSString *)relationshipName ofClass:(Class)objectClass pathPattern:(NSString *)pathPattern onObjectManager:(RKObjectManager *)nilOrObjectManager
{
RKObjectManager *objectManager = nilOrObjectManager ?: [RKObjectManager sharedManager];
RKRoute *route = [objectManager.router.routeSet routeForRelationship:relationshipName ofClass:objectClass method:RKRequestMethodGET];
NSAssert(route, @"Expected to retrieve a route, but got nil");
[objectManager.router.routeSet removeRoute:route];
RKRoute *stubbedRoute = [RKRoute routeWithRelationshipName:relationshipName objectClass:objectClass pathPattern:pathPattern method:RKRequestMethodGET];
[objectManager.router.routeSet addRoute:stubbedRoute];
[self copyFetchRequestBlocksMatchingPathPattern:route.pathPattern toBlocksMatchingRelativeString:pathPattern onObjectManager:objectManager];
return stubbedRoute;
}

+ (void)copyFetchRequestBlocksMatchingPathPattern:(NSString *)pathPattern
toBlocksMatchingRelativeString:(NSString *)relativeString
onObjectManager:(RKObjectManager *)nilOrObjectManager
{
RKObjectManager *objectManager = nilOrObjectManager ?: [RKObjectManager sharedManager];
NSURL *URL = [NSURL URLWithString:pathPattern relativeToURL:objectManager.HTTPClient.baseURL];
for (RKFetchRequestBlock block in objectManager.fetchRequestBlocks) {
NSFetchRequest *fetchRequest = block(URL);
if (fetchRequest) {
// Add a new block that matches our stubbed path
[[RKObjectManager sharedManager] addFetchRequestBlock:^NSFetchRequest *(NSURL *URL) {
// TODO: Note that relativeString does not work because NSURLRequest drops the relative parent of the URL
// if ([[URL relativeString] isEqualToString:relativeString]) {
if ([[URL path] isEqualToString:relativeString]) {
return fetchRequest;
}

return nil;
}];

break;
}
}
}

+ (void)clearCacheDirectory
{
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];

NSError *error = nil;
NSString *cachePath = RKCachesDirectory();
BOOL success = [[NSFileManager defaultManager] removeItemAtPath:cachePath error:&error];
if (success) {
RKLogDebug(@"Cleared cache directory...");
success = [[NSFileManager defaultManager] createDirectoryAtPath:cachePath withIntermediateDirectories:YES attributes:nil error:&error];
if (!success) {
RKLogError(@"Failed creation of cache path '%@': %@", cachePath, [error localizedDescription]);
}
} else {
RKLogError(@"Failed to clear cache path '%@': %@", cachePath, [error localizedDescription]);
}
}

@end
12 changes: 12 additions & 0 deletions RestKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
25079C72151B93DB00266AE7 /* NSEntityDescription+RKAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 25079C6E151B93DB00266AE7 /* NSEntityDescription+RKAdditions.m */; };
25079C76151B952200266AE7 /* NSEntityDescription+RKAdditionsTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 25079C75151B952200266AE7 /* NSEntityDescription+RKAdditionsTest.m */; };
25079C77151B952200266AE7 /* NSEntityDescription+RKAdditionsTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 25079C75151B952200266AE7 /* NSEntityDescription+RKAdditionsTest.m */; };
2507C327161BD5C700EA71FF /* RKTestHelpers.h in Headers */ = {isa = PBXBuildFile; fileRef = 2507C325161BD5C700EA71FF /* RKTestHelpers.h */; settings = {ATTRIBUTES = (Public, ); }; };
2507C328161BD5C700EA71FF /* RKTestHelpers.h in Headers */ = {isa = PBXBuildFile; fileRef = 2507C325161BD5C700EA71FF /* RKTestHelpers.h */; settings = {ATTRIBUTES = (Public, ); }; };
2507C329161BD5C700EA71FF /* RKTestHelpers.m in Sources */ = {isa = PBXBuildFile; fileRef = 2507C326161BD5C700EA71FF /* RKTestHelpers.m */; };
2507C32A161BD5C700EA71FF /* RKTestHelpers.m in Sources */ = {isa = PBXBuildFile; fileRef = 2507C326161BD5C700EA71FF /* RKTestHelpers.m */; };
250CA67D147D8E8B0047D347 /* OCHamcrest.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 250CA67B147D8E800047D347 /* OCHamcrest.framework */; };
250CA67E147D8E8F0047D347 /* OCHamcrestIOS.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 250CA67C147D8E800047D347 /* OCHamcrestIOS.framework */; };
250CA680147D8F050047D347 /* OCHamcrest.framework in CopyFiles */ = {isa = PBXBuildFile; fileRef = 250CA67B147D8E800047D347 /* OCHamcrest.framework */; };
Expand Down Expand Up @@ -645,6 +649,8 @@
25079C6D151B93DB00266AE7 /* NSEntityDescription+RKAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSEntityDescription+RKAdditions.h"; sourceTree = "<group>"; };
25079C6E151B93DB00266AE7 /* NSEntityDescription+RKAdditions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSEntityDescription+RKAdditions.m"; sourceTree = "<group>"; };
25079C75151B952200266AE7 /* NSEntityDescription+RKAdditionsTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSEntityDescription+RKAdditionsTest.m"; sourceTree = "<group>"; };
2507C325161BD5C700EA71FF /* RKTestHelpers.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = RKTestHelpers.h; path = Testing/RKTestHelpers.h; sourceTree = "<group>"; };
2507C326161BD5C700EA71FF /* RKTestHelpers.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = RKTestHelpers.m; path = Testing/RKTestHelpers.m; sourceTree = "<group>"; };
250CA67B147D8E800047D347 /* OCHamcrest.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = OCHamcrest.framework; sourceTree = "<group>"; };
250CA67C147D8E800047D347 /* OCHamcrestIOS.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = OCHamcrestIOS.framework; sourceTree = "<group>"; };
25104F1315C30CD900829135 /* RKSearchWord.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RKSearchWord.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1616,6 +1622,8 @@
25055B8D14EEF40000B9C4DD /* RKMappingTestExpectation.h */,
25055B8E14EEF40000B9C4DD /* RKMappingTestExpectation.m */,
25C954A415542A47005C9E08 /* RKTestConstants.m */,
2507C325161BD5C700EA71FF /* RKTestHelpers.h */,
2507C326161BD5C700EA71FF /* RKTestHelpers.m */,
);
name = Testing;
sourceTree = "<group>";
Expand Down Expand Up @@ -1916,6 +1924,7 @@
253477F915FFBD2E002C0E4E /* NSBundle+RKAdditions.h in Headers */,
25F53F391606269400A093BE /* RKRequestOperationSubclass.h in Headers */,
25A226D61618A57500952D72 /* RKObjectUtilities.h in Headers */,
2507C327161BD5C700EA71FF /* RKTestHelpers.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -2016,6 +2025,7 @@
259B96FA1604CCCC0000C250 /* AFNetworking.h in Headers */,
25F53F3A1606269400A093BE /* RKRequestOperationSubclass.h in Headers */,
25A226D71618A57500952D72 /* RKObjectUtilities.h in Headers */,
2507C328161BD5C700EA71FF /* RKTestHelpers.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -2344,6 +2354,7 @@
259B96F11604CCCC0000C250 /* AFXMLRequestOperation.m in Sources */,
259B96F31604CCCC0000C250 /* UIImageView+AFNetworking.m in Sources */,
25A226D81618A57500952D72 /* RKObjectUtilities.m in Sources */,
2507C329161BD5C700EA71FF /* RKTestHelpers.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -2505,6 +2516,7 @@
259B96F41604CCCC0000C250 /* UIImageView+AFNetworking.m in Sources */,
25E9C8F1161290D500647F84 /* RKObjectParameterization.m in Sources */,
25A226D91618A57500952D72 /* RKObjectUtilities.m in Sources */,
2507C32A161BD5C700EA71FF /* RKTestHelpers.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down

0 comments on commit a10df02

Please sign in to comment.