gpt4 book ai didi

ios - RestKit CoreData 在单元测试期间挂起

转载 作者:行者123 更新时间:2023-11-29 10:52:50 24 4
gpt4 key购买 nike

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

它看起来与 Restkit 中的线程使用有关,因为我已经能够将其缩小到以下类和方法调用。基本上,performBlockAndWait 永远不会返回。我是 Objective 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];

最佳答案

我使用 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"];
}];

关于ios - RestKit CoreData 在单元测试期间挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19694570/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com