XCTests for SLComposeViewController

176 views Asked by At

i was trying to write unit test cases using XCTest for SLComposeViewController and couldn't find any solution so far.Any suggestion in terms of approach and code would be helpful.

My objective is to test the below code using XCTest

SLComposeViewController *tweetSheet = [SLComposeViewController
                                                   composeViewControllerForServiceType:SLServiceTypeTwitter];

            SLComposeViewControllerCompletionHandler __block completionHandler = ^(SLComposeViewControllerResult result)
            {

                [tweetSheet dismissViewControllerAnimated:YES completion:nil];

                switch(result)
                {

                    case SLComposeViewControllerResultCancelled:
                    {

                        [self showAlertWithTitle:nil
                                          andMsg:NSLocalizedString(@"Sharing failed", @"Workout summary text")];

                    }

                        break;

                    case SLComposeViewControllerResultDone:
                    {

                        [self showAlertWithTitle:NSLocalizedString(@"Success", @"Success")
                                          andMsg:NSLocalizedString(@"Message shared", @"Workout summary share text")];

                    }

                        break;

                }

            };
1

There are 1 answers

0
Rob On

If you want to perform an asynchronous test, you create an "expectation" object, an object that sets an expectation that some asynchronous task will be completed at a later point in time. Then in your asynchronous tasks completion block, you fulfill that expectation. Finally, back in the main queue, after you initiate the asynchronous task, you wait for that expectation to be fulfilled.

Thus, putting that all together, the asynchronous test would look like

- (void)testSomethingAsynchronous 
{
    XCTestExpectation *expectation = [self expectationWithDescription:@"some description"];

    [self doSomethingAsynchronousWithCompletionHandler:^{

        // do whatever tests you want

        // when all done, fulfill the expectation

        [expectation fulfill];
    }];

    [self waitForExpectationsWithTimeout:30.0 handler:nil];
}