Error while trying to add a NSPersistentStore (SQLite) within a test

187 views Asked by At

I'm trying to add a few tests to a Core Data app. The App actually works fine, it's the tests that fail miserably when I try to create the stack.

I get the error when trying to add a NSPersistentStore of SQLite type to the NSPersistentStoreCoordinator:

- (NSPersistentStoreCoordinator *)storeCoordinator
{
    if (_storeCoordinator == nil) {
        _storeCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.model];

        NSError *err = nil;
        if (![_storeCoordinator addPersistentStoreWithType:NSSQLiteStoreType
                                             configuration:nil
                                                       URL:self.dbURL
                                                   options:[[self class] persistentStoreCordinatorOptions]
                                                     error:&err]) {
            //This is where I get the error
            NSNotification *note = [NSNotification
                                    notificationWithName:[[self class] persistentStoreCoordinatorErrorNotificationName]
                                    object:self
                                    userInfo:@{@"error" : err}];
            [[NSNotificationCenter defaultCenter] postNotification:note];
            NSLog(@"Error while adding a Store: %@", err);
            return nil;
        }
    }

    return _storeCoordinator;
}

This is the error:

CoreData: error: -addPersistentStoreWithType:SQLite configuration:(null) URL:file:///Users/cfisher/Library/Developer/CoreSimulator/Devices/860C8F97-354D-4A9D-B1E9-CC018680F487/data/Library/Caches/TestModel options:{
    NSInferMappingModelAutomaticallyOption = 1;
    NSMigratePersistentStoresAutomaticallyOption = 1;
    NSReadOnlyPersistentStoreOption = 1;
} ... returned error Error Domain=NSCocoaErrorDomain Code=260 "The operation couldn’t be completed. (Cocoa error 260.)" with userInfo dictionary {
}

The URL mentioned in the error is obtained in the following way (the dbURL property below):

- (void)setUp {
    [super setUp];

    NSURL *cache = [[[NSFileManager defaultManager]
                     URLsForDirectory:NSCachesDirectory
                     inDomains:NSUserDomainMask] lastObject];
    self.dbURL = [cache URLByAppendingPathComponent:self.testModelName];

    self.testModelName = @"TestModel";
    self.testBundle = [NSBundle bundleForClass:[self class]];

}

This only happens within the test. The App works fine. Any ideas?

0

There are 0 answers