Skip to content

Instantly share code, notes, and snippets.

@surpher
Created June 15, 2018 01:28
Show Gist options
  • Save surpher/f09160313137711c39ff9f6a87b6df3a to your computer and use it in GitHub Desktop.
Save surpher/f09160313137711c39ff9f6a87b6df3a to your computer and use it in GitHub Desktop.
XCTest dismiss got called tapping UINavigationBar button
class ViewControllerTests: XCTestCase {
func testDismissGetsCalled() {
let sut = MockViewController()
sut.loadViewIfNeeded()
guard let dismissButton = sut.navigationItem.leftBarButtonItem else { XCTFail("should have a dismiss button"); return }
guard let action = dismissButton.action else { XCTFail("dismiss button should have an action"); return }
sut.performSelector(onMainThread: action, with: dismissButton, waitUntilDone: true)
XCTAssertTrue(sut.dismissedGotCalled)
}
}
extension ViewControllerTests {
class MockViewController: ViewController {
var dismissedGotCalled = false
var completionHandler: (() -> Void)?
override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {
dismissedGotCalled = true
completionHandler?()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment