Permalink
Browse files

Ported RestKit tests over to using the RKTestFactory and completed do…

…cumentation. fixes #318
  • Loading branch information...
1 parent a84f523 commit 868372272fece3b6e6f97abdcd28e17dabf5d852 @blakewatters blakewatters committed Mar 16, 2012
Showing with 325 additions and 368 deletions.
  1. +2 −2 Code/Support/RKFixCategoryBug.h
  2. +52 −47 Code/Testing/RKTestFactory.h
  3. +73 −48 Code/Testing/RKTestFactory.m
  4. BIN Resources/RestKitCoreData.xcdatamodeld/RestKitCoreData.xcdatamodel/elements
  5. BIN Resources/RestKitCoreData.xcdatamodeld/RestKitCoreData.xcdatamodel/layout
  6. +11 −11 Tests/Logic/CoreData/RKInMemoryEntityCacheTest.m
  7. +6 −7 Tests/Logic/CoreData/RKManagedObjectLoaderTest.m
  8. +12 −12 Tests/Logic/CoreData/RKManagedObjectMappingOperationTest.m
  9. +11 −11 Tests/Logic/CoreData/RKManagedObjectMappingTest.m
  10. +1 −1 Tests/Logic/CoreData/RKManagedObjectStoreTest.m
  11. +11 −11 Tests/Logic/CoreData/RKManagedObjectThreadSafeInvocationTest.m
  12. +2 −2 Tests/Logic/CoreData/RKSearchWordObserverTest.m
  13. +1 −1 Tests/Logic/CoreData/RKSearchableManagedObjectTest.m
  14. +4 −4 Tests/Logic/Network/RKAuthenticationTest.m
  15. +2 −4 Tests/Logic/Network/RKClientTest.m
  16. +4 −4 Tests/Logic/Network/RKParamsTest.m
  17. +6 −6 Tests/Logic/Network/RKRequestQueueTest.m
  18. +55 −55 Tests/Logic/Network/RKRequestTest.m
  19. +1 −1 Tests/Logic/Network/RKResponseTest.m
  20. +2 −2 Tests/Logic/Network/RKURLTest.m
  21. +33 −33 Tests/Logic/ObjectMapping/RKObjectLoaderTest.m
  22. +10 −10 Tests/Logic/ObjectMapping/RKObjectManagerTest.m
  23. +2 −2 Tests/Logic/ObjectMapping/RKObjectMappingNextGenTest.m
  24. +4 −4 Tests/Logic/ObjectMapping/RKObjectMappingProviderTest.m
  25. +16 −16 Tests/Logic/ObjectMapping/RKObjectPaginatorTest.m
  26. +2 −9 Tests/RKTestEnvironment.h
  27. +2 −65 Tests/RKTestEnvironment.m
@@ -3,7 +3,7 @@
// RestKit
//
// Created by Blake Watters on 9/1/11.
-// Copyright 2011 Two Toasters
+// Copyright 2011 RestKit
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -21,7 +21,7 @@
#ifndef RestKit_RKCategoryFix_h
#define RestKit_RKCategoryFix_h
-/**
+/*
Add this macro before each category implementation, so we don't have to use
-all_load or -force_load to load object files from static libraries that only contain
categories and no classes.
@@ -27,27 +27,31 @@
Called once per unit testing run when the sharedFactory instance is initialized. RestKit
applications can override via a category.
*/
-- (void)didInitialize;
++ (void)didInitialize;
/**
Application specific customization point for the sharedFactory.
Invoked each time the factory is asked to set up the environment. RestKit applications
leveraging the factory may override via a category.
*/
-- (void)didSetUp;
++ (void)didSetUp;
/**
Application specific customization point for the sharedFactory.
Invoked each time the factory is tearing down the environment. RestKit applications
leveraging the factory may override via a category.
*/
-- (void)didTearDown;
++ (void)didTearDown;
@end
/**
RKTestFactory provides an interface for initializing RestKit
- objects within a unit testing environment.
+ objects within a unit testing environment. The factory is used to ensure isolation
+ between test cases by ensuring that RestKit's important singleton objects are torn
+ down between tests and that each test is working within a clean Core Data environment.
+ Callback hooks are provided so that application specific set up and tear down logic can be
+ integrated as well.
*/
@interface RKTestFactory : NSObject <RKTestFactoryCallbacks>
@@ -56,90 +60,91 @@
///-----------------------------------------------------------------------------
/**
- The baseURL with which to initialize RKClient and RKObjectManager
+ Returns the base URL with which to initialize RKClient and RKObjectManager
instances created via the factory.
+
+ @return The base URL for the factory.
*/
-@property (nonatomic, strong) RKURL *baseURL;
++ (RKURL *)baseURL;
/**
- The class to use when instantiating new client instances.
+ Sets the base URL for the factory.
- **Default**: [RKClient class]
+ @param URL The new base URL.
*/
-@property (nonatomic, strong) Class clientClass;
++ (void)setBaseURL:(RKURL *)URL;
/**
- The class to use when instantiating new object manager instances.
+ Returns the base URL as a string value.
- **Default**: [RKObjectManager class]
+ @return The base URL for the factory, as a string.
*/
-@property (nonatomic, strong) Class objectManagerClass;
-
-///-----------------------------------------------------------------------------
-/// @name Accessing the Shared Factory Instance
-///-----------------------------------------------------------------------------
++ (NSString *)baseURLString;
/**
- Returns the shared test factory object.
+ Sets the base URL for the factory to a new value by constructing an RKURL
+ from the given string.
- @return The shared test factory.
+ @param A string containing the URL to set as the base URL for the factory.
*/
-+ (RKTestFactory *)sharedFactory;
++ (void)setBaseURLString:(NSString *)baseURLString;
///-----------------------------------------------------------------------------
/// @name Building Instances
///-----------------------------------------------------------------------------
/**
- Create and return an RKClient instance.
+ Creates and returns an RKClient instance.
+
+ @return A new client object.
*/
-- (RKClient *)client;
++ (RKClient *)client;
/**
- Create and return an RKObjectManager instance.
+ Creates and returns an RKObjectManager instance.
+
+ @return A new client object.
*/
-- (RKObjectManager *)objectManager;
++ (RKObjectManager *)objectManager;
/**
- Create and return an RKManagedObjectStore instance.
+ Creates and returns a RKManagedObjectStore instance.
+
+ A new managed object store will be configured and returned. If there is an existing
+ persistent store (i.e. from a previous test invocation), then the persistent store
+ is deleted.
+
+ @return A new managed object store object.
*/
-- (RKManagedObjectStore *)objectStore;
++ (RKManagedObjectStore *)managedObjectStore;
+
+///-----------------------------------------------------------------------------
+/// @name Managing Test State
+///-----------------------------------------------------------------------------
/**
Sets up the RestKit testing environment. Invokes the didSetUp callback for application
specific setup.
*/
-- (void)setUp;
++ (void)setUp;
/**
Tears down the RestKit testing environment by clearing singleton instances, helping to
ensure test case isolation. Invokes the didTearDown callback for application specific
cleanup.
*/
-- (void)tearDown;
-
-@end
++ (void)tearDown;
-/**
- The ConvenienceAliases category provides a static interface for performing
- common tasks on the RKTestFactory sharedInstance. All methods defined within the category
- are static aliases for instance method counterparts on [RKTestFactory sharedFactory].
- */
-@interface RKTestFactory (ConvenienceAliases)
+///-----------------------------------------------------------------------------
+/// @name Other Tasks
+///-----------------------------------------------------------------------------
/**
- Ensures the test factory has been initialized
+ Clears the contents of the cache directory by removing the directory and
+ recreating it.
+
+ @see [RKDirectory cachesDirectory]
*/
-+ (void)setUp;
-+ (void)tearDown;
-
-+ (RKURL *)baseURL;
-+ (void)setBaseURL:(RKURL *)URL;
-+ (NSString *)baseURLString;
-+ (void)setBaseURLString:(NSString *)baseURLString;
-
-+ (RKClient *)client;
-+ (RKObjectManager *)objectManager;
-+ (RKManagedObjectStore *)objectStore;
++ (void)clearCacheDirectory;
@end
@@ -8,6 +8,16 @@
#import "RKTestFactory.h"
+@interface RKTestFactory ()
+
+@property (nonatomic, strong) RKURL *baseURL;
+@property (nonatomic, strong) Class clientClass;
+@property (nonatomic, strong) Class objectManagerClass;
+
++ (RKTestFactory *)sharedFactory;
+
+@end
+
static RKTestFactory *sharedFactory = nil;
@implementation RKTestFactory
@@ -16,38 +26,42 @@ @implementation RKTestFactory
@synthesize clientClass;
@synthesize objectManagerClass;
-+ (RKTestFactory *)sharedFactory {
++ (RKTestFactory *)sharedFactory
+{
if (! sharedFactory) {
sharedFactory = [RKTestFactory new];
}
return sharedFactory;
}
-- (id)init {
+- (id)init
+{
self = [super init];
if (self) {
- self.baseURL = [RKURL URLWithString:@"http://localhost:4567"];
+ self.baseURL = [RKURL URLWithString:@"http://127.0.0.1:4567"];
self.clientClass = [RKClient class];
self.objectManagerClass = [RKObjectManager class];
- if ([self respondsToSelector:@selector(didInitialize)]) {
- [self didInitialize];
+ if ([RKTestFactory respondsToSelector:@selector(didInitialize)]) {
+ [RKTestFactory didInitialize];
}
}
return self;
}
-- (RKClient *)client {
+- (RKClient *)client
+{
RKClient *client = [self.clientClass clientWithBaseURL:self.baseURL];
[RKClient setSharedClient:client];
client.requestQueue.suspended = NO;
return client;
}
-- (RKObjectManager *)objectManager {
+- (RKObjectManager *)objectManager
+{
[RKObjectManager setDefaultMappingQueue:dispatch_queue_create("org.restkit.ObjectMapping", DISPATCH_QUEUE_SERIAL)];
[RKObjectMapping setDefaultDateFormatters:nil];
RKObjectManager *objectManager = [self.objectManagerClass managerWithBaseURL:self.baseURL];
@@ -60,68 +74,79 @@ - (RKObjectManager *)objectManager {
return objectManager;
}
-- (RKManagedObjectStore *)objectStore {
- RKManagedObjectStore *store = [RKManagedObjectStore objectStoreWithStoreFilename:@"RKTests.sqlite"];
- [store deletePersistantStore];
- [RKManagedObjectStore setDefaultObjectStore:store];
-
- return store;
-}
-
-- (void)setUp {
- if ([self respondsToSelector:@selector(didSetUp)]) {
- [self didSetUp];
- }
-}
-
-- (void)tearDown {
- [RKObjectManager setSharedManager:nil];
- [RKClient setSharedClient:nil];
- [RKManagedObjectStore setDefaultObjectStore:nil];
-
- if ([self respondsToSelector:@selector(didTearDown)]) {
- [self didTearDown];
- }
-}
+#pragma mark - Public Static Interface
-@end
-
-@implementation RKTestFactory (ConvenienceAliases)
-
-+ (void)setUp {
- [[RKTestFactory sharedFactory] setUp];
-}
-
-+ (RKURL *)baseURL {
++ (RKURL *)baseURL
+{
return [RKTestFactory sharedFactory].baseURL;
}
-+ (void)setBaseURL:(RKURL *)URL {
++ (void)setBaseURL:(RKURL *)URL
+{
[RKTestFactory sharedFactory].baseURL = URL;
}
-+ (NSString *)baseURLString {
++ (NSString *)baseURLString
+{
return [[[RKTestFactory sharedFactory] baseURL] absoluteString];
}
-+ (void)setBaseURLString:(NSString *)baseURLString {
++ (void)setBaseURLString:(NSString *)baseURLString
+{
[[RKTestFactory sharedFactory] setBaseURL:[RKURL URLWithString:baseURLString]];
}
-+ (id)client {
++ (id)client
+{
return [[RKTestFactory sharedFactory] client];
}
-+ (id)objectManager {
++ (id)objectManager
+{
return [[RKTestFactory sharedFactory] objectManager];
}
-+ (id)objectStore {
- return [[RKTestFactory sharedFactory] objectStore];
++ (id)managedObjectStore
+{
+ RKManagedObjectStore *store = [RKManagedObjectStore objectStoreWithStoreFilename:@"RKTests.sqlite"];
+ [store deletePersistantStore];
+ [RKManagedObjectStore setDefaultObjectStore:store];
+
+ return store;
}
-+ (void)tearDown {
- [[RKTestFactory sharedFactory] tearDown];
++ (void)setUp
+{
+ if ([self respondsToSelector:@selector(didSetUp)]) {
+ [self didSetUp];
+ }
+}
+
++ (void)tearDown
+{
+ [RKObjectManager setSharedManager:nil];
+ [RKClient setSharedClient:nil];
+ [RKManagedObjectStore setDefaultObjectStore:nil];
+
+ if ([self respondsToSelector:@selector(didTearDown)]) {
+ [self didTearDown];
+ }
+}
+
++ (void)clearCacheDirectory
+{
+ NSError* error = nil;
+ NSString* cachePath = [RKDirectory cachesDirectory];
+ BOOL success = [[NSFileManager defaultManager] removeItemAtPath:cachePath error:&error];
+ if (success) {
+ RKLogInfo(@"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
Oops, something went wrong.

0 comments on commit 8683722

Please sign in to comment.