Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9101575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:09:58+00:00 2026-06-17T01:09:58+00:00

According to NSManagedObjectContext Class Documentation … – (NSArray *)executeFetchRequest:(NSFetchRequest *)request error:(NSError **)error Return Value

  • 0

According to NSManagedObjectContext Class Documentation…

- (NSArray *)executeFetchRequest:(NSFetchRequest *)request error:(NSError **)error

Return Value

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.

I’m trying to create a unit test for the situation “if an error occurs, returns nil.”

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…

- (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");
}

Is there a simple way to trigger a fetch request returning nil?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T01:09:59+00:00Added an answer on June 17, 2026 at 1:09 am

    I had the same conundrum. I like to keep unit test coverage at 100% whenever possible. There is no easy way to generate an organic error condition. In fact, I’m not sure the current implementation of the 4 store types that come with Core Data will ever trigger an error in response to executeFetchRequest:error. But as it could happen in the future, here is what I did:

    I have one unit test case file that is dedicated to validating how my classes handle errors populated by executeFetchRequest:error. I define a subclass of NSIncrementalStore that always produces an error during requests in the implementation file. [NSManagedObjectContext executeFetchRequest:error] is processed by [NSPersistentStoreCoordinator executeRequest:withContext:error:] which processes [NSPersistentStore executeRequest:withContext:error:] on all stores. You may notice that the word “fetch” drops when you move to the coordinator – saves and fetch requests are handled by the same method executeRequest:withContext:error:. So I get coverage for testing against save errors and fetch requests by defining a NSPersistentStore that will always respond to saves and fetches with errors.

    #define kErrorProneStore @"ErrorProneStore"
    @interface ErrorProneStore : NSIncrementalStore
    
    
    @end
    
    @implementation ErrorProneStore
    
    - (BOOL)loadMetadata:(NSError **)error
    {
        //Required - Apple's documentation claims you can omit setting this, but I had memory allocation issues without it. 
        NSDictionary * metaData = @{NSStoreTypeKey : kErrorProneStore, NSStoreUUIDKey : @""};
        [self setMetadata:metaData];
        return YES;
    }
    -(void)populateError:(NSError **)error
    {
        if (error != NULL)
        {
            *error = [[NSError alloc] initWithDomain:NSCocoaErrorDomain
                                                code:NSPersistentStoreOperationError
                                            userInfo:nil];
        }
    }
    - (id)executeRequest:(NSPersistentStoreRequest *)request
             withContext:(NSManagedObjectContext *)context
                   error:(NSError **)error
    {
        [self populateError:error];
        return nil;
    }
    - (NSIncrementalStoreNode *)newValuesForObjectWithID:(NSManagedObjectID *)objectID
                                             withContext:(NSManagedObjectContext *)context
                                                   error:(NSError **)error
    {
        [self populateError:error];
        return nil;
    }
    - (id)newValueForRelationship:(NSRelationshipDescription *)relationship
                  forObjectWithID:(NSManagedObjectID *)objectID
                      withContext:(NSManagedObjectContext *)context
                            error:(NSError **)error
    {
        [self populateError:error];
        return nil;
    }
    - (NSArray *)obtainPermanentIDsForObjects:(NSArray *)array
                                        error:(NSError **)error
    {
        [self populateError:error];
        return nil;
    }
    @end
    

    Now you can construct the Core Data stack using the ErrorProneStore and be guaranteed your fetch requests will return nil and populate the error parameter.

    - (void)testFetchRequestErrorHandling
    {
        NSManagedObjectModel * model = [NSManagedObjectModel mergedModelFromBundles:nil];
    
        [NSPersistentStoreCoordinator registerStoreClass:[ErrorProneStore class]
                                            forStoreType:kErrorProneStore];
    
        NSPersistentStoreCoordinator * coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
    
    
        NSManagedObjectContext * context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
        [context setPersistentStoreCoordinator:coordinator];
        [coordinator addPersistentStoreWithType:kErrorProneStore
                                  configuration:nil
                                            URL:nil
                                        options:nil
                                          error:nil];
    
        NSFetchRequest * request = [NSFetchRequest fetchRequestWithEntityName:@"AValidEntity"];
    
        NSError * error;
        [context executeFetchRequest:request
                               error:&error];
    
        STAssertNotNil(error, @"Error should always be nil");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

According to the Ruby Set class's documentation, == Returns true if two sets are
According to the Best Practices section of the MSDN documentation for the System.Enum class:
According documentation: System.Array.Sort<T> - sorts the elements in an entire System.Array using the System.IComparable
According to BeautifulSoup documentation , it is possible to get the value of tag's
According to PHP.net manual, pg_pconnect will create a persistent connection, or will return the
According to Hibernate documentation : After observing that arrays cannot be lazy , you
According to the official documentation, when there is a new RawContact inserted to the
according to the twitter api documentation http://dev.twitter.com/doc/get/statuses/user_timeline accessing the current logged in users timeline
According to the C++0x spec , the following is legal class A { A(int
According to the official documentation , the KeyDown event on a Windows Forms control

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.