If I understand your problem correctly, the problem is that even after calling [MagicalRecord setupCoreDataStackWithInMemoryStore]; in setUp tests access a real data store?
I had the same problem when writing unit tests for objects responsible for interacting with the database, and with a little debugging, I think I found a problem.
[MagicalRecord setupAutoMigratingCoreDataStack]; c - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions called earlier [MagicalRecord setupCoreDataStackWithInMemoryStore]; in setUp . And if we look at how the main data stack works with MagicalRecord, weβll understand what the problem is.
+ (void) setupCoreDataStackWithInMemoryStore; { if ([NSPersistentStoreCoordinator MR_defaultStoreCoordinator] != nil) return; NSPersistentStoreCoordinator *coordinator = [NSPersistentStoreCoordinator MR_coordinatorWithInMemoryStore]; [NSPersistentStoreCoordinator MR_setDefaultStoreCoordinator:coordinator]; [NSManagedObjectContext MR_initializeDefaultContextWithCoordinator:coordinator]; }
[NSPersistentStoreCoordinator MR_defaultStoreCoordinator] non-zero at the storage setup point in memory and does the method above to silently return without any action. And at this moment you are left with your real data store instead of the storage in memory. The solution is simple. Just call
[MagicalRecord cleanUp]
before calling [MagicalRecord setupCoreDataStackWithInMemoryStore]; in setUp to break the current data stack and really set up the storage in memory so it looks like this:
- (void)setUp{ [super setUp]; [MagicalRecord setDefaultModelFromClass:[self class]]; [MagicalRecord cleanUp]; [MagicalRecord setupCoreDataStackWithInMemoryStore]; }
source share