RestKit CoreData在unit testing期间挂起

我正在尝试为使用Restkit执行与我们的应用程序需求相关的各种CoreData和JSON映射操作的服务类进行测试。 通过iphone模拟器部署运行时,该服务工作正常,但在通过unit testing的上下文运行时挂起。

它看起来与Restkit中的线程使用有关,因为我已经能够将其缩小到以下类和方法调用。 基本上,performBlockAndWait永远不会返回。 我对客观世界很新(不是一般的发展),所以任何帮助都将不胜感激。

Restkit类:RKFetchRequestManagedObjectCache

方法:

- (NSSet *)managedObjectsWithEntity:(NSEntityDescription *)entity attributeValues:(NSDictionary *)attributeValues inManagedObjectContext:(NSManagedObjectContext *)managedObjectContext ... // test hangs on this fetch call [managedObjectContext performBlockAndWait:^{ objects = [managedObjectContext executeFetchRequest:fetchRequest error:&error]; }]; 

我正在使用以下内容设置我的CoreData测试堆栈:

 NSBundle *bundle = [NSBundle bundleForClass:NSClassFromString(@"EventServiceTests")]; NSLog(@"Found bundle: %@", bundle); NSString *bundlePath = [bundle pathForResource:@"EventDataModel" ofType:@"momd"]; NSLog(@"Creating model from path: %@", bundlePath); NSURL *momURL = [NSURL URLWithString:bundlePath]; NSLog(@"URL for model: %@", momURL); NSManagedObjectModel *model = [[NSManagedObjectModel alloc] initWithContentsOfURL:momURL]; RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel]; NSLog(@"Initializing the Core Data stack..."); [managedObjectStore createPersistentStoreCoordinator]; NSString* dataStorePath = [RKApplicationDataDirectory() stringByAppendingPathComponent: @"EventDataModel.dat"]; NSLog(@"Persistent store file path: %@", dataStorePath); NSURL *storeUrl = [NSURL fileURLWithPath: dataStorePath]; if (![managedObjectStore.persistentStoreCoordinator addPersistentStoreWithType:NSBinaryStoreType configuration:nil URL:storeUrl options:nil error:&error]) { NSLog(@"Issue creating persitent store: %2@", error); } NSAssert(managedObjectStore.persistentStoreCoordinator.persistentStores, @"Failed to add persistent store: %@", error); [managedObjectStore createManagedObjectContexts]; NSLog(@"Setting the default store shared instance to: %@", managedObjectStore); [RKManagedObjectStore setDefaultStore:managedObjectStore]; NSLog(@"Configuring the object manager..."); RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://eventconsole.eng.techtarget.com/"]]; objectManager.managedObjectStore = managedObjectStore; NSLog(@"Setting shared manager instance to: %@", objectManager); [RKObjectManager setSharedManager:objectManager]; 

然后执行请求操作:

 NSString* url = UPCOMING_EVENTS_URL_PATH; NSLog(@"Attempting to get upcoming events from url: %@", url); [[RKObjectManager sharedManager] getObjectsAtPath:url parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { NSLog(@"Successfully loaded %@ upcoming events", [NSString stringWithFormat:@"%ld", (unsigned long)[mappingResult count]] ); returnVal = TRUE; } failure:^(RKObjectRequestOperation *operation, NSError *error) { NSLog(@"Error loading upcoming events: %@", error); returnVal = FALSE; } ]; 

和实际的测试代码:

 NSLog(@"Executing testLoadAttendees..."); [_eventService loadAttendees:@"2269"]; [NSThread sleepForTimeInterval:5.0f]; NSOperationQueue* queue = [RKObjectRequestOperation responseMappingQueue]; [queue waitUntilAllOperationsAreFinished]; 

我找到了一个使用RestKit提供的实用程序测试类之一的解决方案。

 RKTestNotificationObserver *observer = [RKTestNotificationObserver notificationObserverForName:RKObjectRequestOperationDidFinishNotification object:nil]; observer.timeout = 60; [observer addObserver]; NSLog(@"Executing testLoadAttendees..."); [_eventService loadAttendees:@"2269"]; [observer waitForNotification]; 

我用实用方法包装的:

 - (void)executeAndTimeoutAfterSeconds:(int) timeoutSeconds usingBlock:(void(^)())block { RKTestNotificationObserver *observer = [RKTestNotificationObserver notificationObserverForName:RKObjectRequestOperationDidFinishNotification object:nil]; [observer addObserver]; observer.timeout = timeoutSeconds; block(); [observer waitForNotification]; } 

所以测试现在执行使用:

 [self executeAndTimeoutAfterSeconds:60 usingBlock:^ { NSLog(@"Executing testLoadAttendees..."); [_eventService loadAttendees:@"2269"]; }];