本文介绍了是否可以测试IBAction?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
对IBOutlets进行单元测试有点容易,但是IBActions呢?我试图找到一种方法,但是没有任何运气.有什么方法可以在View Controller中的IBAction与nib文件中的按钮之间进行单元测试连接的单元测试?
It is kinda easy to unit test IBOutlets, but how about IBActions? I was trying to find a way how to do it, but without any luck. Is there any way to unit test connection between IBAction in a View Controller and a button in the nib file?
推荐答案
我使用OCMock做到了,就像这样:
I did it using OCMock, like this:
MyViewController *mainView = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
[mainView view];
id mock = [OCMockObject partialMockForObject:mainView];
//testButtonPressed IBAction should be triggered
[[mock expect] testButtonPressed:[OCMArg any]];
//simulate button press
[mainView.testButton sendActionsForControlEvents: UIControlEventTouchUpInside];
[mock verify];
如果未连接IBAction,则测试将失败,并显示错误未调用预期方法".
If IBAction is not connected, the test will fail with error "expected method was not invoked".
这篇关于是否可以测试IBAction?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!