Help us understand the problem. What is going on with this article?

ユニットテスト時に専用のAppDelegateクラスにすげ替える。

More than 1 year has passed since last update.

はじめに

iOSのユニットテストを行う際、テストターゲットアプリを指定した方が便利です。ただ、プロダクトコードのAppDelegateには大概色々のもの(UI、プッシュ、行動解析ライブラリの初期化やら。。。)がごちゃごちゃ乗っていてユニットテストの阻害要因になりやすいす。

解決策

ユニットテスト用のAppDelegateを作って、ユニットテストを実行する際はそれにすげ替えましょう!

実際のコード

こんな感じになります。なお、TestableAppDelegateはテストターゲットのみに含めます。

main.m
int main(int argc, char * argv[])
{
   @autoreleasepool {

        Class appDelegateClass = NSClassFromString(@"TestableAppDelegate");
        if (!appDelegateClass) {
            appDelegateClass = [AppDelegate class];
        }

        return UIApplicationMain(argc, argv, nil, NSStringFromClass(appDelegateClass));
    }
}

テスト用のAppDelegateはこんな感じですっからかんにします。

TestableAppDelegate.m
@implementation TestableAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = [UIViewController new];
    [self.window makeKeyAndVisible];

    [UIView setAnimationsEnabled:NO];

    return YES;
}

@end

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away