アプリケーション デリゲート セッターをスウィズルすることで、デリゲート オブジェクトをモックし、applicationDidBecomeActive などの特定の期待値を検証することができました。
id<UIApplicationDelegate> delegate = [MyCustomDelegate alloc] init];
UIApplication *app = [UIApplication alloc] init];
app.delegate = delegate;
//results in "There can only be one UIApplication" error
その代わり:
@implementation AppDelegateTests {
- (void)testAppDelegate {
//perform swizzle on [UIApplication class] and method @selector(setDelegate:) with
//[self class] and at the bottom @selector(swizzledSetDelegate:)
id<UIApplicationDelegate> delegate = [MyCustomDelegate alloc] init];
//Here's the magic, this line actually calls [UIApplication setDelegate] and not the swizzledSetDelegate method below.
//If you don't understand this, look up how swizzling works and hopefully you can wrap your head around it. The logic is quite mind-bendy.
[self swizzledSetDelegate:delegate];
//here set up your mock on the delegate and verify state of things
}
- (void)swizzledSetDelegate:(id<UIApplicationDelegate>)delegate {
//do nothing
}
}
この例は、私がテストする必要があるものにかなりカスタム化されていることに注意してください。何をモックしたいのか、それが可能かどうかを考える必要があります。