LoginSignup
10
10

More than 5 years have passed since last update.

Storyboardを使ったアプリをGHUnitでテストするときviewDidLoadは呼ばれない

Last updated at Posted at 2014-02-28

Storyboard上のビューを以下のようにテストする際、viewDidLoadやviewWillAppearは呼ばれない。

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
    GHAssertNotNil(storyboard, @"ストーリーボードが存在しない");
    self.vc = [storyboard instantiateViewControllerWithIdentifier:@"hoge"]; // Storyboard ID
    GHAssertNotNil(self.vc, @"ビューコントローラが存在しない");
    [self.vc loadView];

したがって、呼ぶ必要がある場合は明示的に呼ばなければならない。

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
    GHAssertNotNil(storyboard, @"ストーリーボードが存在しない");
    self.vc = [storyboard instantiateViewControllerWithIdentifier:@"hoge"]; // Storyboard ID
    GHAssertNotNil(self.vc, @"ビューコントローラが存在しない");
    [self.vc loadView];
    [self.vc viewWillAppear];  // ここ
    [self.vc viewDidLoad];  // ここ

もしくは以下のように、ビューにinitWithCoder:メソッドを生やし、そこにコードを書く。

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        /*
         URLデータを読み込む
        */
        NSBundle* bundle = [NSBundle mainBundle];
        NSString* path = [bundle pathForResource:@"URLs" ofType:@"plist"];
        urls = [NSArray arrayWithContentsOfFile:path];
    }
    return self;
}
10
10
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
10
10