Here is what I use in Swift. I created a helper function that I can use in all my unit tests of the UIViewController:
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 call it from the test as follows:
func testScheduleActionIsConnected() { XCTAssertTrue(checkActionForOutlet(controller.btnScheduleOrder, actionName: "scheduleOrder", event: UIControlEvents.TouchUpInside, controller: controller )) }
Basically, I'm sure the btnScheduleOrder button has an IBAction associated with the scheduleOrder name for the TouchUpInside event. I need to pass the controller where the button is contained as a way to check the target for the action.
You can also make it a bit more complicated by adding another else clause in case the Button doesn't expand, which means the outlet is not there. Since I like to separate output tests and actions that I haven't included here,
source share