|
| 1 | +// |
| 2 | +// KIFTestStep+RestKit.h |
| 3 | +// RestKit |
| 4 | +// |
| 5 | +// Created by Blake Watters on 7/1/13. |
| 6 | +// Copyright (c) 2009-2013 RestKit. All rights reserved. |
| 7 | +// |
| 8 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | +// you may not use this file except in compliance with the License. |
| 10 | +// You may obtain a copy of the License at |
| 11 | +// |
| 12 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | +// |
| 14 | +// Unless required by applicable law or agreed to in writing, software |
| 15 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | +// See the License for the specific language governing permissions and |
| 18 | +// limitations under the License. |
| 19 | +// |
| 20 | + |
| 21 | +#import <SystemConfiguration/SystemConfiguration.h> |
| 22 | +#import <MobileCoreServices/MobileCoreServices.h> |
| 23 | +#import "KIFTestStep.h" |
| 24 | +#import <RestKit/RestKit.h> |
| 25 | +#import <RestKit/CoreData.h> |
| 26 | + |
| 27 | +/** |
| 28 | + The `RestKit` category provides a number of steps to aid in testing RestKit based applications using the KIF integration testing library. |
| 29 | + */ |
| 30 | +@interface KIFTestStep (RestKit) |
| 31 | + |
| 32 | +///------------------------------------ |
| 33 | +/// @name Stubbing Network Interactions |
| 34 | +///------------------------------------ |
| 35 | + |
| 36 | +/** |
| 37 | + Creates and returns a KIF test step that sets the `suspended` property on `[RKObjectManager sharedManager].operationQueue` to the given value. |
| 38 | + |
| 39 | + @param suspended A Boolean value specifying if the queue should be set to suspended or not. |
| 40 | + @return A KIF test step for setting the value of the suspended property for the operation queue of the RestKit shared object manager instance. |
| 41 | + */ |
| 42 | ++ (instancetype)stepToSetSuspendedForRestKitSharedObjectManagerOperationQueue:(BOOL)suspended; |
| 43 | + |
| 44 | +/** |
| 45 | + Creates and returns a KIF test step that stubs the value of the `networkReachabilityStatus` property on `[RKObjectManager sharedManager].HTTPClient` to the given value and emits a notification that network reachability state has transitioned to the given value. This step is commonly used to test offline mode or application behavior during network availability transitions. |
| 46 | + |
| 47 | + @param reachabilityStatus An `AFNetworkReachabilityStatus` value for the desired reachability state to stub on the HTTP client of the shared manager. |
| 48 | + @return A KIF test step for stubbing the value of the network reachability status property for the HTTP client of the RestKit shared object manager instance. |
| 49 | + */ |
| 50 | ++ (instancetype)stepToStubReachabilityStatusOfRestKitSharedObjectManagerHTTPClient:(AFNetworkReachabilityStatus)reachabilityStatus; |
| 51 | + |
| 52 | +/** |
| 53 | + Creates and returns a KIF test step that stubs a named route registered on `[RKObjectManager sharedManager].router.routeSet` to return a new path pattern. |
| 54 | + |
| 55 | + @param routeName The name of the route to be stubbed. |
| 56 | + @param pathPattern The new path pattern value to be used by the specified route. |
| 57 | + @return A KIF test step for stubbing the path pattern of a named route registered on the RestKit shared object manager instance. |
| 58 | + */ |
| 59 | ++ (instancetype)stepToStubRouteOfRestKitSharedObjectManagerNamed:(NSString *)routeName toPathPattern:(NSString *)pathPattern; |
| 60 | + |
| 61 | +/** |
| 62 | + Creates and returns a KIF test step that that stubs a class route registered on `[RKObjectManager sharedManager].router.routeSet` to return a new path pattern. |
| 63 | + |
| 64 | + @param objectClass The class of the route to be stubbed. |
| 65 | + @param method The exact method bitmask value of the route to be stubbed. |
| 66 | + @param pathPattern The new path pattern value to be used by the specified route. |
| 67 | + @return A KIF test step for stubbing the path pattern of a class route registered on the RestKit shared object manager instance. |
| 68 | + */ |
| 69 | ++ (instancetype)stepToStubRouteOfRestKitSharedObjectManagerForClass:(Class)objectClass method:(RKRequestMethod)method toPathPattern:(NSString *)pathPattern; |
| 70 | + |
| 71 | +/** |
| 72 | + Creates and returns a KIF test step that stubs a relationship route registered on `[RKObjectManager sharedManager].router.routeSet` to return a new path pattern. |
| 73 | + |
| 74 | + @param objectClass The class of the route to be stubbed. |
| 75 | + @param method The exact method bitmask value of the route to be stubbed. |
| 76 | + @param pathPattern The new path pattern value to be used by the specified route. |
| 77 | + @return A KIF test step for stubbing the path pattern of a relationship route registered on the RestKit shared object manager instance. |
| 78 | + */ |
| 79 | ++ (instancetype)stepToStubRouteOfRestKitSharedObjectManagerForRelationship:(NSString *)relationshipName ofClass:(Class)objectClass method:(RKRequestMethod)method toPathPattern:(NSString *)pathPattern; |
| 80 | + |
| 81 | +///---------------------------- |
| 82 | +/// @name Caching Response Data |
| 83 | +///---------------------------- |
| 84 | + |
| 85 | +/** |
| 86 | + Creates and returns a KIF test step that caches a response for a URL with the given path relative to `[RKObjectManager sharedManager].baseURL` for the specified HTTP method that will return the specified response data. |
| 87 | + |
| 88 | + @param path A relative path for constructing the destination URL. |
| 89 | + @param method A single, specific request method for requests to the URL that should load the cached response (i.e. `RKRequestMethodGET`). |
| 90 | + @param responseData The data to be stored as the body of the cached response. |
| 91 | + @return A KIF test step that caches the specified response. |
| 92 | + */ |
| 93 | ++ (instancetype)stepToCacheResponseForURLRelativeToRestKitSharedObjectManagerWithPath:(NSString *)path method:(RKRequestMethod)method responseData:(NSData *)responseData; |
| 94 | + |
| 95 | +/** |
| 96 | + Creates and returns a KIF test step that caches a response for a URL with the given path relative to `[RKObjectManager sharedManager].baseURL` for the specified HTTP method that will return response data loaded from the fixture at the specified path. |
| 97 | + |
| 98 | + @param path A relative path for constructing the destination URL. |
| 99 | + @param method A single, specific request method for requests to the URL that should load the cached response (i.e. `RKRequestMethodGET`). |
| 100 | + @param responseData The data to be stored as the body of the cached response. |
| 101 | + @return A KIF test step that caches the specified response. |
| 102 | + */ |
| 103 | ++ (instancetype)stepToCacheResponseForURLRelativeToRestKitSharedObjectManagerWithPath:(NSString *)path method:(RKRequestMethod)method responseDataFromContentsOfFixtureAtPath:(NSString *)fixturePath; |
| 104 | + |
| 105 | +///------------------------------------- |
| 106 | +/// @name Creating Objects via Factories |
| 107 | +///------------------------------------- |
| 108 | + |
| 109 | +/** |
| 110 | + Creates and returns a KIF test step that will create an object from the RestKit factory with the given name, optionally setting a dictionary of property values on the constructed object and yielding it to a configuration block for further processing. |
| 111 | + |
| 112 | + @param name The name of the RestKit factory construct the object from. |
| 113 | + @param properties A dictionary of property values to assign to the constructed object. |
| 114 | + @param configurationBlock An optional block to yield the constructed object to for configuration. |
| 115 | + @return A KIF test step that creates an object from a RestKit factory. |
| 116 | + */ |
| 117 | ++ (instancetype)stepToCreateObjectFromRestKitFactoryWithName:(NSString *)name properties:(NSDictionary *)properties configurationBlock:(void (^)(id object))configurationBlock; |
| 118 | + |
| 119 | +/** |
| 120 | + Creates and returns a KIF test step that will create an arbitrary number of objects from a list of named factories. |
| 121 | + |
| 122 | + @param names An array of strings that specify the names of RestKit factories that should be invoked to create objects. |
| 123 | + @return An array of `KIFTestStep` objects that create objects via RestKit factories. |
| 124 | + */ |
| 125 | ++ (NSArray *)stepsToCreateObjectsFromRestKitFactoriesWithNames:(NSArray *)names; |
| 126 | + |
| 127 | +///--------------------------------- |
| 128 | +/// @name Interacting with Core Data |
| 129 | +///--------------------------------- |
| 130 | + |
| 131 | +/** |
| 132 | + Creates and returns a KIF test step that will insert a new `NSManagedObject` instance for the entity with the given name into `[RKManagedObjectStore defaultStore].mainQueueManagedObjectContext`, optionally yielding it to a block for further configuration and saving it to the persistent store. |
| 133 | + |
| 134 | + @param entityName The entity for the newly inserted managed object. |
| 135 | + @param persisted Whether or not the newly inserted object should be saved to the persistent store. |
| 136 | + @param configurationBlock An optional block to yield the constructed object to for configuration. |
| 137 | + @return A KIF test step that inserts an object into the default managed object store. |
| 138 | + */ |
| 139 | ++ (instancetype)stepToInsertManagedObjectInRestKitDefaultManagedObjectStoreWithEntityName:(NSString *)entityName savedToPersistentStore:(BOOL)persisted configurationBlock:(void (^)(id managedObject))configurationBlock; |
| 140 | + |
| 141 | +/** |
| 142 | + Creates and returns a KIF test step that will delete all managed objects for the entity with the given name from `[RKManagedObjectStore defaultStore].mainQueueManagedObjectContext`. |
| 143 | + |
| 144 | + @param entityName The name of the entity to delete all managed objects of. If `nil`, then all managed objects will be deleted. |
| 145 | + @return A KIF test step that deletes managed objects from the default managed object store. |
| 146 | + */ |
| 147 | ++ (instancetype)stepToDeleteAllManagedObjectsInRestKitDefaultManagedObjectStoreWithEntityName:(NSString *)entityName; |
| 148 | + |
| 149 | +/** |
| 150 | + Creates and returns a KIF test step that will perform a block within the `[RKManagedObjectStore defaultStore].mainQueueManagedObjectContext` and save the context, optionally back to the persistent store. |
| 151 | + |
| 152 | + @param block A block to be executed within the managed object context. It accepts two arguments: the managed object context itself and a pointer to a Boolean value that determines if the subsequent save of the managed object context will be back to the persistent store. The block can be `nil` if you only wish to trigger a save. The default value of `saveToPersistentStore` is `NO`. |
| 153 | + */ |
| 154 | ++ (instancetype)stepToPerformBlockAndSaveMainQueueManagedObjectContextOfRestKitDefaultManagedObjectStore:(void(^)(NSManagedObjectContext *managedObjectContext, BOOL *saveToPersistentStore))block; |
| 155 | + |
| 156 | +@end |
| 157 | + |
| 158 | +@interface KIFTestStep (ViewControllers) |
| 159 | + |
| 160 | +/** |
| 161 | + Sets the default `UINavigationBar` subclass to use when presenting view controllers without a navigation bar class specified. |
| 162 | + |
| 163 | + @param navigationBarClass A subclass of `UINavigationBar` to use when presenting view controllers via `stepToPresentViewControllerWithClass:withinNavigationControllerWithNavigationBarClass:toolbarClass:configurationBlock:` when the `navigationBarClass` argument is `nil`. |
| 164 | + */ |
| 165 | ++ (void)setDefaultNavigationBarClass:(Class)navigationBarClass; |
| 166 | + |
| 167 | +/** |
| 168 | + Sets the default `UIToolbar` subclass to use when presenting view controllers without a toolbar bar class specified. |
| 169 | + |
| 170 | + @param toolbarClass A subclass of `UIToolbar` to use when presenting view controllers via `stepToPresentViewControllerWithClass:withinNavigationControllerWithNavigationBarClass:toolbarClass:configurationBlock:` when the `toolbarClass` argument is `nil`. |
| 171 | + */ |
| 172 | ++ (void)setDefaultToolbarClass:(Class)toolbarClass; |
| 173 | + |
| 174 | +/** |
| 175 | + Creates and returns a KIF test step that will instantiate and present an instance of the specified `UIViewController` subclass within a `UINavigationController` instance with the specified `UINavigationBar` and `UIToolbar` subclasses, optionally yielding the instantiated controller to the block for configuration. |
| 176 | + |
| 177 | + @param viewControllerClass The `UIViewController` subclass to instantiate. |
| 178 | + @param navigationBarClass A subclass of `UINavigationBar` to use when instantiating the `UINavigationController` instance within which the view controller instance will be presented. If `nil`, then the class specified via `setDefaultNavigationBarClass:` will be used. |
| 179 | + @param toolbarClass A subclass of `UIToolbar` to use when instantiating the `UINavigationController` instance within which the view controller instance will be presented. If `nil`, then the class specified via `setDefaultToolbarClass:` will be used. |
| 180 | + @param configurationBlock An optional block in which to yield the newly instantiated view controller instance prior to presenting it in the main window. |
| 181 | + */ |
| 182 | ++ (instancetype)stepToPresentViewControllerWithClass:(Class)viewControllerClass withinNavigationControllerWithNavigationBarClass:(Class)navigationBarClass toolbarClass:(Class)toolbarClass |
| 183 | + configurationBlock:(void (^)(id viewController))configurationBlock; |
| 184 | +@end |
0 commit comments