用Cedar的iOStesting控制器
我想用Cedartesting一个控制器,但不能真正理解为什么它不工作。 控制器永远不会显示,viewDidLoad或viewDidAppear永远不会被调用。 这是锡达本来不是要做的,还是我的错误?
describe(@"MyController", ^{ __block UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil]; __block UINavigationController *root = (UINavigationController *)[[[[UIApplication sharedApplication] delegate]window ]rootViewController]; __block MyViewController *model = [storyboard instantiateViewControllerWithIdentifier:@"MyController"]; [root pushViewController:model animated:YES]; it(@"should test something", ^{ expect(model.content).to(be_truthy); }); });
unit testing同步运行。 任何(或可能)animation在正常的unit testing中都不起作用,因为testing将在更改发生之前完成。
它看起来像你试图testing你的视图控制器的状态,当它显示。 在这种情况下,我们所做的不是推动它,而是加载它:
[model view];
这将加载故事板的视图,然后调用它的-viewDidLoad
。 那么你应该能够testing它的状态。
我不使用Cedar,但是我有一个基于OCUnit的视图控制器的testing驱动开发屏幕截图: http : //qualitycoding.org/uiviewcontroller-tdd/
(顺便说一下,“模型”对于控制器来说是一个非常混乱的名字。)
我通常testing我的视图控制器孤立与像这样的设置:
beforeEach(^{ window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; subject = [storyboard instantiateViewControllerWithIdentifier:@"ViewControllerName"]; window.rootViewController = subject; [window makeKeyAndVisible]; subject.view should_not be_nil; }];