Note that there are some explanatory texts on larger screens.

plurals
  1. POCore Data Unit Testing - Unsure how to trigger error case in executeFetchRequest:error:
    text
    copied!<p>According to <a href="https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/CoreDataFramework/Classes/NSManagedObjectContext_Class/NSManagedObjectContext.html">NSManagedObjectContext Class Documentation</a>...</p> <pre><code>- (NSArray *)executeFetchRequest:(NSFetchRequest *)request error:(NSError **)error </code></pre> <blockquote> <p><em>Return Value</em></p> <p>An array of objects that meet the criteria specified by request fetched from the receiver and from the persistent stores associated with the receiver’s persistent store coordinator. If an error occurs, returns nil. If no objects match the criteria specified by request, returns an empty array.</p> </blockquote> <p>I'm trying to create a unit test for the situation "if an error occurs, returns nil."</p> <p>I would like to stay away from using OCMock (or subclassing NSManagedObjectContext to override the executeFetchRequest:error: method) because I figure there's an easy way to ensure failure of this method. So far my unit test reads...</p> <pre><code>- (void)testReportingCoreDataErrorToDelegate { NSManagedObjectContext *badContext = [[NSManagedObjectContext alloc] init]; [bcc setManagedObjectContext:badContext]; [bcc fetchFromCoreData]; STAssertTrue([mockDelegate didReceiveCoreDataError], @"This never asserts, it fails because the fetch request couldn't find an entity name - i.e. no managed object model"); } </code></pre> <p>Is there a simple way to trigger a fetch request returning nil?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload