iPhone测试:测试IBOutlets [英] iPhone testing: testing IBOutlets

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

问题描述

我问这个问题实际上让我有些尴尬,因为它听起来很基本,但是已经困扰了我好几个小时...

I actually feel a little embarrassed asking this because it feels about as basic as you can go, but it's been stumping me for hours...

假设我有一个UIViewController实例(称为MyViewController),该实例仅向UITextField包含一个IBOutlet,并且在页面加载时我将其设置为Enabled或不基于BOOL属性.

Let's say I have an instance of a UIViewController (called MyViewController) which simply has one IBOutlet to a UITextField, and when the page loads I either set it to be Enabled or not based on a BOOL property.

- (void)viewDidLoad {
    [super viewDidLoad];
    self.surname.enabled = self.allowTextField;
}

一切都正确连接,并且当我运行该应用程序时,它可以按预期运行(主-我希望如此,因为如果我无法正确完成这一部分,我可能不应该编写iPhone应用程序...) .

Everything is wired up correctly and when I run the app it works as expected (lord - I'd hope so cause if I couldn't get that part right, I probably shouldn't be writing iPhone apps...).

无论如何,我决定要勤奋工作,为应用程序编写应用程序单元测试,因为这是我想变得更加扎实的东西.不幸的是,我一生无法理解以下原因为何不起作用...

Anyway, I decided to be diligent and write application Unit Tests for the app because it's something I want to become a lot more solid in. Unfortunately, I cannot for the life of me understand why the following does not work...

-(void)testFieldEnabled {
    MyViewController *myController = [[MyViewController alloc] initWithNibName:nil bundle:nil];
    myController.allowTextField = YES;
    [myController viewDidLoad];
     STAssertTrue(myController.surname.enabled, @"Why is this not correct?");
}

我使这个示例非常简单,因为我真的不明白为什么它不起作用,并认为这一定是由于我对单元测试的理解出现重大故障.上面的单元测试不应该启动MyViewController页面的实例,设置属性,运行方法并返回true吗?此刻,它一直告诉我未启用姓氏文本字段.

I've kept this example extremely simple because I truly don't get why this is not working and believe it must be due a major failing in my understanding of unit testing. Shouldn't the above unit test fire-up an instance of MyViewController page, set the property, run the method and return true? At the moment it keeps telling me that the surname textfield is not enabled.

感谢您的帮助,祝大家新年快乐!

Thanks for your help and a happy new year to you all!

推荐答案

这是因为手动调用viewDidLoad并不会真正加载视图.您应该调用视图控制器的view属性,该属性将自动加载视图(或手动调用loadView).

This is because calling viewDidLoad manually doesn't actually load the view. You should call view property of the view controller, which will load the view automatically (or call loadView manually).

这篇关于iPhone测试:测试IBOutlets的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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