使用Cedar的iOS测试控制器 [英] iOS testing controllers with Cedar

查看:118
本文介绍了使用Cedar的iOS测试控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Cedar测试控制器,但无法真正理解为什么它不起作用.永远不会显示控制器,永远不会调用viewDidLoad或viewDidAppear.这是Cedar不想做的事情,还是我的错?

I'm trying to test a controller(s) with Cedar but can't really understand why it's not working. The controller never gets shown, viewDidLoad or viewDidAppear are never called. Is this something Cedar wasn't meant to do or just my mistake?

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 tests run synchronously. Anything that is — or can be — animated won't work in a normal unit test, because the test will be done before the change takes place.

看起来好像您在尝试尝试显示视图控制器的状态.在这种情况下,我们要做的不是推送它,而是加载它:

It looks like you're trying to test the state of your view controller when it is shown. In that case, what we do is not push it, but load it:

[model loadViewIfNeeded];

这将从故事板上加载视图,然后调用其-viewDidLoad.然后,您应该能够测试其状态.

This will load up the view from the story board, then invoke its -viewDidLoad. You should then be able to test its state.

我不使用Cedar,但是我确实有一个基于OCUnit的截屏视频,用于测试驱动的视图控制器开发:

I don't use Cedar, but I do have an OCUnit-based screencast of test-driven development of a view controller: How to Do UIViewController TDD

(顺便说一下,模型"是控制器的一个非常混乱的名称.)

("model" is a very confusing name for a controller, by the way.)

这篇关于使用Cedar的iOS测试控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆