Reputation: 4748
Here is my code:
WDAlarm *alarm = [NSEntityDescription insertNewObjectForEntityForName:@"WDAlarm"
inManagedObjectContext:[[UTCoreData sharedManager] managedObjectContext]];
I am checking the insertedObjects using a breakpoint and:
po [[[UTCoreData sharedManager] managedObjectContext] insertedObjects];
the result is:
{(
)}
{(
)}
The UTCoreData is a helper class (instead of adding all core data code inside app delegate). The object alarm is created. According to Apple's documentation right here: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/CoreDataFramework/Classes/NSManagedObjectContext_Class/NSManagedObjectContext.html#//apple_ref/occ/instm/NSManagedObjectContext/reset the insertedObjects
Returns the set of objects that have been inserted into the receiver but not yet saved in a persistent store.
which is exactly my case, however it keeps on returning empty set (and the notification NSManagedObjectContextObjectsDidChangeNotification
is not posted).
Update 1: The sharedManager is working fine:
(lldb) po [[UTCoreData sharedManager] managedObjectContext] <NSManagedObjectContext: 0x920fff0>
(lldb) po [[UTCoreData sharedManager] managedObjectContext] <NSManagedObjectContext: 0x920fff0>
Update 2: The code works fine in the production code, not in the XCTestCase. I don't know why it won't although everything is created as expected.
Upvotes: 1
Views: 217
Reputation: 3360
You are running in a classic dependency issue when your unit tests rely on a singleton and you are not testing the singleton itself.
As Dan Shelly already mentioned, unit testing Core Data can be difficult. There are some best practices how to test Core Data. The one that I use in my projects for years is to create a new Core Data stack with an in-memory store in each setUp
method. Reset/remove the Core Data stack in each tearDown
method to ensure that you have a fresh and clean stack for each unit test method.
Upvotes: 1