测试已测试 | ✗ |
Lang语言 | Obj-CObjective C |
许可证 | MIT |
发布最新发布 | 2014 年 12 月 |
由 James Richard 维护。
为 XCTest 实现的 Assert Eventually
将 KZAssertEventually 中的文件复制到您的单元测试包中。需要 OCHamcrest 和 XCTest。
有时您会在异步执行操作的方法或例程上编写测试。xctest-assert-eventually 允许运行循环继续处理事件,同时阻塞测试代码,直到您从块中返回 true 或达到超时时间。例如
__block BOOL test = NO;
dispatch_async(dispatch_get_main_queue(), ^{
test = YES;
});
assertEventuallyWithBlock(^{
return test;
});
// Continue with other assertions
有时您可能想要等待特定条件,然后对该条件相关的内容进行断言。例如,一个 UIView 成为可见的,然后对其其他属性进行断言。以下是一个使用嵌套 OCHamcrest 匹配器的示例,尽管您也可以使用 XCTest 断言
assertEventuallyWithBlock(^{
if (view.isHidden == NO) {
assertThat(view.superview, sameInstance(otherView))
assertThatFloat(view.bounds.size.height, greaterThan(@0))
assertThatFloat(view.bounds.size.width, greaterThan(@0))
return YES;
}
return NO;
});
默认超时时间为 2 秒。要调整它,请使用 assertEventuallyWithBlockAndTimeout 方法,并将秒数作为第二个参数设置超时时间。
assertEventuallyWithBlockAndTimeout(^{ return bool;}, 10);
如果您想使用 Hamcrest 匹配器而不是块匹配器,语法如下
assertEventuallyThat(&object, equalTo(otherObject));
带有超时的:
assertEventuallyThat(&object, equalTo(otherObject), 10);
以下是一个更完整的示例,其中我们使用 GCD 在一秒后使断言通过。
__block NSString *string;
double delayInSeconds = 1.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
string = @"test";
});
assertEventuallyThat(&string, equalTo(@"test"));