Is it possible to test IBAction?

后端 未结 7 774
深忆病人
深忆病人 2021-01-05 13:50

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 bet

7条回答
  •  生来不讨喜
    2021-01-05 14:23

    Julio Bailon's answer above translated for Swift 3:

        func checkActionForButton(_ button: UIButton?, actionName: String, event: UIControlEvents = UIControlEvents.touchUpInside, target: UIViewController) -> Bool {
    
        if let unwrappedButton = button, let actions = unwrappedButton.actions(forTarget: target, forControlEvent: event) {
    
            var testAction = actionName
            if let trimmedActionName = actionName.components(separatedBy: ":").first {
                testAction = trimmedActionName
            }
    
            return (!actions.filter { $0.contains(testAction) }.isEmpty)
        }
    
        return false
    }
    

提交回复
热议问题