繁体   English   中英

如何等待 animation 从单元测试完成

[英]How to wait for animation to finish from unit tests

我正在使用 Xcode 的 OCUnit 编写单元测试,我的测试调用了一个执行 animation 的方法。 我希望只有在 animation 完成后才能继续测试方法。

执行 animation 的方法没有 animation 委托,我认为它不应该有。

在不设置 animation 代表的情况下,如何等待 animation 以测试方法结束?

您不想真正等待 animation; 这将花费 animation 运行所需的时间。 如果你有几千个测试,这可以加起来。

更有效的方法是将UIView static方法在一个类中mock出来,使其立即生效。 然后将该文件包含在您的测试目标(但不是您的应用程序目标)中,以便该类别仅编译到您的测试中。 我们用:

#import "UIView+SpecFlywheel.h"

@implementation UIView (SpecFlywheel)

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

@end

以上只是立即执行 animation 块,如果也提供了完成块,则立即执行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM