XCTests for SLComposeViewController

我与影子孤独终老i 提交于 2019-12-12 01:59:18

问题


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:


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];
}


来源:https://stackoverflow.com/questions/26901797/xctests-for-slcomposeviewcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!