When unit testing components which use Core Data for storing and loading objects I encountered the following problem:
Fetching properties setting NSFetchRequest's resultType to NSDictionaryResultType. The result is always empty. Although there are values available as show by other tests of the same components. As well, during normal App execution the fetch requests return adequate results.
For completeness I might comment, that for the unit tests I setup Core Data (in - (void)setUp) with Magical Record like [MagicalRecord setupCoreDataStackWithInMemoryStore];. Then I set the NSManagedObjectContext of the component under test to the one returned by [NSManagedObjectContext MR_defaultContext]. After creating the test data I call save on the context.
There are two scenarios:
A) Calling [componentUnderTest.managedObjectContext save:nil] or [NSManagedObjectContext MR_saveOnlySelfAndWait] after having created the test data. Then, when the fetch request in question is executed, the returned array is empty.
B) Calling [componentUnderTest.managedObjectContext MR_saveToPersistentStoreAndWait] after creation of test data. When the fetch request is executed (executeFetchRequest:error:) the following exception is thrown: -[__NSTaggedDate count]: unrecognized selector sent to instance 0xe41b1dde2f600000
Note: The property (there is just one) I want to load is of type NSDate. I've tried setting NSStrings, NSExpressionDescriptionss and NSPropertyDescriptions via propertiesToFetch: on the fetch request instance. The result is always the same.
Aucun commentaire:
Enregistrer un commentaire