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

  • Home
  • SEARCH
  • 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 8971991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:06:09+00:00 2026-06-15T18:06:09+00:00

I have a unit test module derived from SenTestCase . I would like to

  • 0

I have a unit test module derived from SenTestCase. I would like to have a unit test method that would load a UIDocument derived document I saved on the application sandbox previously. Note this test is about loading the document locally (iCloud is not configured). I know openWithCompletionHandler runs asynchronously so I understand this would never work as soon as the test function runs off the stack. Below code is given to indicate my intention (of course it doesn’t work):

-(void)testLoadingDocument{
    ...
    MyDocument *document = [[MyDocument alloc] initWithFileURL:destUrl];
    STAssertNotNil(document, @"Document is nil");

    NSLog(@"LOAD: %@", document.fileURL);
    [document openWithCompletionHandler:^(BOOL success) {
        NSLog(@"openWithCompletionHandler success = %@", success);
        if (success) {
            // document.packet will be filled by loadFromContents
            STAssertNotNil(document.packet, @"document.packet is nil.");
        }
    }];
}

My question is really is there any way to test openWithCompletionHandler from within unit testing framework? I don’t mind if I have to run the entire document loading operation synchronously within a block of code. Since this is a test code I thought this would be acceptable unlike code that has to run asynchronously on the device.

Many thanks in advance.

  • 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-15T18:06:11+00:00Added an answer on June 15, 2026 at 6:06 pm

    This problem took me a while to figure out. I love unit testing, but when using SenTestCase your tests do not run in the same environment your regular code would. Most importantly, you lack a main loop that has a run loop in it, making anything with async call backs prone to doing nothing.

    So what’s the solution? Provide a run loop ourselves and have it run until the block gets called. We use a __block variable that we set in the completion block to see when we can stop running the run loop.

    -(void)testOfAsyncCallingMethod{
    
        __block bool wasCalled = NO;
    
        [testingObject methodThatRunsACompletionBlock:^{
            wasCalled = YES;
        }];
    
        NSDate *loopUntil = [NSDate dateWithTimeIntervalSinceNow:10];
        while (wasCalled == NO && [loopUntil timeIntervalSinceNow] > 0) {
            [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
                                     beforeDate:loopUntil];
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a unit test that creates a mock calls my method to be
I have a unit test that fails because headers are already sent. However, the
I have a unit test that I am writing and have run into an
I have a unit test where I run a method for every item in
I have a unit test that tests if an Exception is throw, but this
I have a unit test like so: [Test] public void DataIn_NoOfRowsReached_CreatesSequentialData() { //Assert MyLogic
I have a module that uses std::string as part of its interface. I would
How would you Unit Test the update process of an application? I have an
I'm trying to write a unit test for a module that will give me
I have the following HttpModule that I wanted to unit test. Problem is I

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.