LoginSignup
6
6

More than 5 years have passed since last update.

テストの中でストーリーボードを使い、さらに非同期な動作をおこなわせたいとき

Last updated at Posted at 2014-04-08

ストーリーボードを使ったテストはshouldRunOnMainThreadをYESにしてメインスレッドで実行すればよいが、そのテストの中に非同期な動作が入っている場合、shouldRunOnMainThreadをYESにしてもうまくいかない。

そのようなときは下記のようにする。

ViewControllerTest.m

- (BOOL)shouldRunOnMainThread
{
//    return YES;
    return NO;
}

// 各々のテストが実行される前に呼ばれる
- (void)setUp
{
    /*
     ストーリーボードの初期化

     ビューのオブジェクトを作るので、メインループで実行しなければならない
     */
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
    GHAssertNotNil(storyboard, @"ストーリーボードが存在しない");
    self.vc = [storyboard instantiateViewControllerWithIdentifier:@"MeasureCertainNetworking"]; // Storyboard ID
    GHAssertNotNil(self.vc, @"ビューコントローラが存在しない");
//    [self.vc loadView];
    /*
     メインループで実行する別のやり方
     */
    [self.vc performSelectorOnMainThread:@selector(loadView) withObject:nil waitUntilDone:YES];
}

これでビューコントローラのloadViewはメインスレッドでおこなわれ、別の非同期なメソッドはメインスレッド以外のスレッドで動作する。

6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6