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
Here is what I use in Swift. I created a helper function that I can use in all my UIViewController unit tests:
func checkActionForOutlet(outlet: UIButton?, actionName: String, event: UIControlEvents, controller: UIViewController)->Bool{
if let unwrappedButton = outlet {
if let actions: [String] = unwrappedButton.actionsForTarget(controller, forControlEvent: event)! as [String] {
return(actions.contains(actionName))
}
}
return false
}
And then I just invoke it from the test like this:
func testScheduleActionIsConnected() {
XCTAssertTrue(checkActionForOutlet(controller.btnScheduleOrder, actionName: "scheduleOrder", event: UIControlEvents.TouchUpInside, controller: controller ))
}
I am basically making sure that the button btnScheduleOrder has an IBAction associated with the name scheduleOrder for the event TouchUpInside. I need to pass the controller where the button is contained as a way to verify the target for the action as well.
You can also make it a little more sophisticated by adding some other else clause in case the unwrappedButton does not exist which means the outlet is not there. As I like to separate outlets and actions tests I don't have it included here