如何在单元测试中点击UIImageView

时间:2016-05-18 17:43:48

标签: ios iphone swift

我有一个位于导航栏左侧的UIImageView。我如何在XCTestCase中点击imageView?

1 个答案:

答案 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,以了解其被点击的时间。

我希望这对你有所帮助。