1

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

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

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];
4

1 回答 1

2

我想出了一个使用 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"];
}];
于 2013-10-31T15:29:26.260 回答