我有一个位于导航栏左侧的UIImageView。我如何在XCTestCase中点击imageView?
答案 0 :(得分:0)
根据您的评论,假设您有UIBarButtonItem
UIImage
并且您已经定义了一些@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
后,您无需为其中的UITapGestureRecognizer
定义UIImage
,以了解其被点击的时间。
我希望这对你有所帮助。