我正在编写测试以验证在进行某个操作时某个类型的新视图控制器是否被推送到导航控制器的堆栈上。但是,我发现调用pushViewController
不会导致我的UIViewController
视图控制器列表被更新,而前一个视图控制器也不会被新推送的控制器。
这是我的测试类的缩写版本:
#import "RRViewController.h"
#import "RestroomDetailsViewController.h"
@implementation RRViewControllerTests
{
RRViewController *viewController;
...
UINavigationController *navigationController;
}
- (void)setUp
{
[super setUp];
viewController = [[RRViewController alloc] init];
...
navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
}
...
- (void)testSelectingRestroomPushesNewViewController
{
[viewController userDidSelectRestroom];
UIViewController *currentTopViewController = navigationController.topViewController;
XCTAssertFalse([currentTopViewController isEqual:viewController], @"New view controller should be pushed onto Navigation Controller stack.");
XCTAssertTrue([currentTopViewController isKindOfClass:[RestroomDetailsViewController class]], @"New view Controller should be an RestroomDetailsViewController.");
}
这是RRViewController
类,其中userDidSelectRestroomn
方法调用了viewController
:
#import "RestroomDetailsViewController"
...
- (void)userDidSelectRestroom
{
RestroomDetailsViewController *nextViewController = [[RestroomDetailsViewController alloc] init];
[[self navigationController] pushViewController:nextViewController animated:YES];
}
问题是我的两个测试都失败了。在navigationController.viewControllers
被调用后,查看RRViewControllerTests
测试类中的userDidSelectRestroom
会显示:
<__NSArrayI 0x7ffaf9d37020>(
<RRViewController: 0x7ffaf9eba420>
)
即。只是原始的根RRViewController
。
有关为什么新RestroomDetailsViewController
没有被推到navigationController
堆叠的任何想法?