Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Unit Testing with Animations

Why does the following unit test pass using Xcode 5.0 and XCTesting? I mean, I understand the bottom line: 1 == 0 is not evaluated. But why is it not evaluated? How can I make this perform so that it would fail?

- (void)testAnimationResult
{
    [UIView animateWithDuration:1.5 animations:^{
        // Some animation
    } completion:^(BOOL finished) {
        XCTAssertTrue(1 == 0, @"Error: 1 does not equal 0, of course!");
    }];
}
like image 485
MikeS Avatar asked Oct 29 '13 20:10

MikeS


1 Answers

This will, technically, work. But of course the test will sit and run for 2 seconds. If you have a few thousand tests, this can add up.

More effective is to stub out the UIView static method in a category so that it takes effect immediately. Then include that file in your test target (but not your app target) so that the category is compiled into your tests only. We use:

#import "UIView+Spec.h"

@implementation UIView (Spec)

#pragma mark - Animation
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion {
    if (animations) {
        animations();
    }
    if (completion) {
        completion(YES);
    }
}

@end

The above simply executes the animation block immediately, and the completion block immediately if it's provided as well.

like image 165
tooluser Avatar answered Sep 29 '22 09:09

tooluser