ios - 如何在单元测试中点击 UIImageView

标签 ios iphone swift

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

最佳答案

根据您的评论,假设您有一个带有 UIImageUIBarButtonItem,并且您已经为点击时定义了一些 @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/

相关文章:

ios - Swift 对象永远无法将值放入其中

swift - 在字典扩展中引用自己

ios - 使用 Tableview 告诉 ios 中的用户最近对服务器的调用没有获取任何数据

iphone - iOS 模拟器在虚拟机中崩溃

javascript - 触摸两个 dom 元素上的事件?

iphone - 使用标签以编程方式创建后在 iOS 中移动多个图像

ios - 取消 NSOperationQueue

ios - 如何在swift中进行多重继承?

ios - ReactiveCocoa 中的内存管理

ios - AWSCognitoIdentityMultiFactorAuthentication 永远不会返回