Skip to content

Instantly share code, notes, and snippets.

@giovani-pereira-ifood
Last active May 6, 2021 15:45
Show Gist options
  • Save giovani-pereira-ifood/af0571fbd4d2ed548285a3cba45139ed to your computer and use it in GitHub Desktop.
Save giovani-pereira-ifood/af0571fbd4d2ed548285a3cba45139ed to your computer and use it in GitHub Desktop.
// On the view's code
func setupGestures() {
let tap = UITapGestureRecognizer(target: self, action: #selector(callDelegateTap))
addGestureRecognizer(tap)
}
// On the test code
func test_tappingView_shouldCallDelegate() {
let sut = MyView()
let gesture = sut.gestureRecognizers?.first { $0 is UITapGestureRecognizer }
let target = (gesture?.value(forKey: "_targets") as? [NSObject])?.first
let selectorString = String(describing: target)
.components(separatedBy: ", ")
.first?
.replacingOccurrences(of: "(action=", with: "")
.replacingOccurrences(of: "Optional(", with: "") ?? ""
sut.perform(.init(stringLiteral: selectorString))
XCTAssertTrue(delegateSpy.viewTappedCalled)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment