是否可以测试IBAction?

编程入门 行业动态 更新时间:2024-10-27 04:37:43
本文介绍了是否可以测试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?

本文发布于:2023-11-16 18:54:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1606928.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:测试   IBAction

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!