使用动画进行iOS单元测试

Mik*_*keS 6 xcode unit-testing ios xctest

为什么使用Xcode 5.0和XCTesting进行以下单元测试?我的意思是,我理解底线:1 == 0未评估.但为什么不进行评估呢?如何才能使其失败?

- (void)testAnimationResult
{
    [UIView animateWithDuration:1.5 animations:^{
        // Some animation
    } completion:^(BOOL finished) {
        XCTAssertTrue(1 == 0, @"Error: 1 does not equal 0, of course!");
    }];
}
Run Code Online (Sandbox Code Playgroud)

too*_*ser 9

从技术上讲,这将起作用.但当然测试将持续2秒.如果你有几千个测试,这可以加起来.

更有效的是在类别中存根UIView静态方法,以便它立即生效.然后在测试目标中包含该文件(但不包括您的应用程序目标),以便仅将类别编译到测试中.我们用:

#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
Run Code Online (Sandbox Code Playgroud)

上面只是立即执行动画块,如果也提供了立即完成块.