SLComposeViewController的XCTests [英] XCTests for SLComposeViewController
问题描述
我正在尝试使用XCTest for SLComposeViewController编写单元测试用例,但到目前为止找不到任何解决方案.关于方法和代码的任何建议都将有所帮助.
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.
我的目标是使用XCTest测试以下代码
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;
}
};
推荐答案
如果要执行异步测试,请创建一个期望"对象,该对象设置了一些异步任务将在以后完成的期望时间点.然后,在您的异步任务完成块中,您可以实现期望.最后,回到主队列中,在启动异步任务之后,您等待该期望得到满足.
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];
}
这篇关于SLComposeViewController的XCTests的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!