【问题标题】:XCTests for SLComposeViewControllerSLComposeViewController 的 XCTests
【发布时间】:2015-01-10 04:19:33
【问题描述】:

我正在尝试使用 XCTest 为 SLComposeViewController 编写单元测试用例,但到目前为止找不到任何解决方案。任何关于方法和代码的建议都会有所帮助。

我的目标是使用 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;

                }

            };

【问题讨论】:

    标签: ios iphone xctest slcomposeviewcontroller


    【解决方案1】:

    如果您想执行异步测试,您可以创建一个“期望”对象,该对象设置某个异步任务将在稍后时间点完成的期望。然后在你的异步任务完成块中,你实现了这个期望。最后,回到主队列,在启动异步任务后,等待该期望得到满足。

    因此,将所有这些放在一起,异步测试看起来像

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

    【讨论】:

      猜你喜欢
      • 2017-12-07
      • 1970-01-01
      • 1970-01-01
      • 2016-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-31
      相关资源
      最近更新 更多