用KIF测试异步UITableView



我刚开始使用KIF,在用当前配置测试异步加载的表视图时遇到了问题。

我的应用程序中有一个带按钮的主屏幕。当按下该按钮时,将显示一个模式视图控制器。

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Setup accessibility
    self.theTableView.accessibilityLabel = @"My List";
    // Register for notifications
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(objectsLoadedNotification:) name:kNotificationObjectsLoaded object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(objectsFailedToLoadNotification:) name:kNotificationObjectsFailedToLoad object:nil];
    // Start loading new data
    [[MyListObjectManager sharedInstance] requestObjects];
}

现在,我在KIF中设置了一个测试,看起来像这样:

+ (id)scenarioToSelecList
{
    KIFTestScenario *scenario = [KIFTestScenario scenarioWithDescription:@"Test that a user can select an item from my list."];
    [scenario addStep:[KIFTestStep stepToTapViewWithAccessibilityLabel:@"List"]];
    [scenario addStep:[KIFTestStep stepToWaitForNotificationName:kNotificationObjectsLoaded object:nil]];
    [scenario addStep:[KIFTestStep stepToTapRowInTableViewWithAccessibilityLabel:@"My List" atIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]]];
}

当我运行测试时,KIF从未看到我的对象加载通知。

在调试过程中,我用异步调用替换了viewDidLoad方法中的[objectManagerrequestObjects]调用,以便在三秒钟后请求对象:

[[MyListObjectManager sharedInstance] performSelector:@selector(requestObjects) withObject:nil afterDelay:3.0];

当我这样做时,我在KIF输出中看到以下内容:

PASS (0.90s): Tap view with accessibility label "Find Books"
PASS (3.02s): Wait for notification "notificationObjectsLoaded"

这让我相信最初的问题是,我正在等待的通知在第一步执行完成之前就被触发了。

那么,问题就变成了,为什么第一步需要0.9秒才能完成?它是否正在等待模态动画完成,然后再从步骤返回?在这种情况下,加载对象的请求比动画完成得更快。

KIF应如何处理?或者有没有一种更合适的方法来为我的表视图加载异步数据?

您的怀疑可能是正确的,即在运行侦听通知的步骤之前,通知已启动,但我在您的输出中注意到,步骤正在成功

在任何情况下,如果您正在将项目加载到表视图中,那么点击其中一个项目的步骤将一直等待到加载超时,并且您可以更改超时。如果您完全删除等待通知的步骤,您可能会发现您的测试工作正常。

最新更新