[英]popViewControllerAnimated:YES doesn't work
I'm writing a unit test and I find popViewControllerAnimated:YES doesn't work. 我正在编写单元测试,但发现popViewControllerAnimated:YES不起作用。
(void)testNavi {
UINavigationController *navi = [[UINavigationController alloc] init];
UIViewController *controllerA = [[UIViewController alloc] initWithNibName:nil bundle:nil];
UIViewController *controllerB = [[UIViewController alloc] initWithNibName:nil bundle:nil];
[navi pushViewController:controllerA animated:NO];
[navi pushViewController:controllerB animated:NO];
[navi popViewControllerAnimated:YES];
XCTAssertEqual(navi.topViewController, controllerA);
}
If I change [navi popViewControllerAnimated:YES] into [navi popViewControllerAnimated:NO], it works. 如果我将[navi popViewControllerAnimated:YES]更改为[navi popViewControllerAnimated:NO],则可以使用。 I don't know why.
我不知道为什么
You'd better to call popViewControllerAnimated after view was created. 创建视图后,最好调用popViewControllerAnimated。 So you may make subclass of NavigationController and implements it like this.
因此,您可以制作NavigationController的子类并像这样实现它。
- (void)init {
self = [super init];
if (self) {
UIViewController *controllerA = [[UIViewController alloc] initWithNibName:nil bundle:nil];
UIViewController *controllerB = [[UIViewController alloc] initWithNibName:nil bundle:nil];
self.viewControllers = @[controllerA, controllerB];
}
return self;
}
- (void)viewWillAppear {
[self popViewControllerAnimated:YES];
}
This is because the animation happens asynchronously on another thread. 这是因为动画是异步发生在另一个线程上的。 The animation to pop the view controller takes a small amount of time, but the test assertion is made before the animation has completed, so the
topViewController
has not changed yet. 弹出视图控制器的动画需要花费少量时间,但是测试断言是在动画完成之前进行的,因此
topViewController
尚未更改。
The best way to test this it to mock the UINavigationController
and verify that popViewController:YES
is called. 测试它的最佳方法是模拟
UINavigationController
并验证是否调用了popViewController:YES
。
I had a similar problem testing pushViewController
when the push is animated. 推送动画时,我在测试
pushViewController
时遇到了类似的问题。
I was able to solve this by wrapping my assertion in this handy delay
function from matt. 通过将断言包装在来自matt的此便捷
delay
函数中,我能够解决此问题。
When introducing a delay in your test, it is necessary to use expectations to ensure the assertions are checked. 在测试中引入延迟时,有必要使用期望值以确保检查了断言。 Otherwise, the test completes before the delay code executes, resulting in a passed test that did not check your assertions!
否则,测试将在延迟代码执行之前完成,从而导致通过的测试未检查您的断言!
// code to push the view controller goes here...
// set up the expectation
let expectation = expectationWithDescription("anything you want")
// now, assert something about the navigation controller
// after waiting 1/10th second for the animation to finish
delay (0.1) {
// for example: Navigation controller should have the
// expected number of view controllers
XCTAssertEqual(1, navigationController.viewControllers.count)
// if we get this far, then assertions passed and we must
// fulfill expectations to pass the test
expectation.fulfill()
}
// wait longer than the delay value, so the code in the delay closure
// has time to finish
waitForExpectationsWithTimeout(0.2, handler: nil)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.