我有一个位于导航栏左侧的 UIImageView。我将如何在 XCTestCase 中点击 imageView?
最佳答案
根据您的评论,假设您有一个带有 UIImage
的 UIBarButtonItem
,并且您已经为点击时定义了一些 @IBAction
在您的 UIBarButtonItem
中,您可以像这样在 UIBarButtonItem
中模拟点击:
@IBOutlet weak var barButtonItem: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
// Send the the action to invoke the target specified when you make your @IBAction or define a target manually.
UIApplication.sharedApplication().sendAction(barButtonItem.action, to: barButtonItem.target, from: nil, forEvent: nil)
}
// @IBAction defined using Interface Builder
@IBAction func buttonTapped(sender: AnyObject) {
print("Tapped")
}
在上面的代码中,当 UIViewController
加载时,您将在控制台中看到输出 "Tapped"
。当您初始化要测试的 UIViewController
时,可以将相同的逻辑转换为您的单元测试。
一旦定义了 UIBarBUttonItem
,就不必为其中的 UIImage
定义 UITapGestureRecognizer
来知道它何时被点击。
希望对你有帮助
关于ios - 如何在单元测试中点击 UIImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37306754/