XCTests for SLComposeViewController

2019-09-09 22:02发布

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条回答
在下西门庆
2楼-- · 2019-09-09 22:41

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];
}
查看更多
登录 后发表回答