【问题标题】:How to write test case for UIViewAnimation in iOS如何在 iOS 中为 UIView Animation 编写测试用例
【发布时间】:2015-08-13 16:33:35
【问题描述】:

我想为动画块写测试用例。

-(void)removeFooter {
    [UIView animateWithDuration:0.3 animations:^{
        self.contentOffset = CGPointMake(0, 0);
    } completion:^(BOOL finished) {
        if (!self.loadMoreDelegate) {
            self.tableFooterView = nil;
        }
    }];
}

在测试用例中需要检查tablefooterview,但是self.tableFooterView = nil;在完成块中的一段时间后执行,而测试用例在它之前运行。请告诉我如何为此编写测试用例。

【问题讨论】:

    标签: ios objective-c animation tdd testcase


    【解决方案1】:

    您可以从竞赛块中调用“测试用例”方法

    请记住,它通常会在该线程中执行。

    但是如果你想在 main 中执行它,你可以简单地以这种方式调用该方法:

    -(void)removeFooter {
        [UIView animateWithDuration:0.3 animations:^{
            self.contentOffset = CGPointMake(0, 0);
        } completion:^(BOOL finished) {
            if (!self.loadMoreDelegate) {
                self.tableFooterView = nil;
            }
        [self performSelectorOnMainThread:@selector(myMethod:) withObject:nil waitUntilDone:YES];
        }];
    }
    

    【讨论】:

    • 但不起作用。我写了以下代码 -(void)testloadMoreDelegate { self.tableView.loadMoreDelegate = self; XCTAssert(self.tableView.tableFooterView, @"页脚应该被初始化"); [self.tableView setLoadMoreDelegate:nil]; [self performSelectorOnMainThread:@selector(checkTableFooter) withObject:nil waitUntilDone:YES]; } -(void)checkTableFooter { XCTAssertNil(self.tableView.tableFooterView, @"页脚应该被初始化"); }
    • 这段代码和你在问题中写的真的不一样;你想达到什么目标?
    • 我试着向你解释。您的完成块是异步执行的。这意味着,在您的代码中,您创建动画并启动它,但动画中的代码将在另一个线程中执行,因此竞赛块,在 0.3 秒后(如您设置的那样)......但是应用程序不等待动画执行 0.3 秒;它将继续执行其余代码的其他操作...我想您应该阅读有关多线程概念的更多信息
    【解决方案2】:

    我得到了解决方案。 在anthor线程中执行任务,并让主线程休眠直到任务完成。

     NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    
        [queue addOperationWithBlock:^{
           // task which take time to execute 
            self.tableView.refreshDelegate = self;
            [self.tableView failedToRefreshWithMessage:@"test message"];
        }];
        sleep(2.5); // greater than task completion time
        XCTAssertFalse(self.tableView.isPullToRefresh,@"refresh control not stop.");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多