Last active
September 10, 2017 22:39
-
-
Save joemasilotti/5dd13766d23df729e405bf865e6d176a to your computer and use it in GitHub Desktop.
Testing View Controllers in Swift 3.0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
protocol Controller: class { | |
var navigationController: NavigationController? { get } | |
func present(wrappedController controller: Controller, animated flag: Bool, completion: Completion?) | |
func present(controller: Controller, animated flag: Bool, completion: Completion?) | |
func dismissController(animated flag: Bool, completion: Completion?) | |
} | |
extension Controller where Self: UIViewController { | |
func present(wrappedController controller: Controller, animated flag: Bool, completion: Completion?) { | |
guard let controller = controller as? UIViewController else { return } | |
let navigationController = UINavigationController(rootViewController: controller) | |
present(navigationController, animated: flag, completion: completion) | |
} | |
func present(controller: Controller, animated flag: Bool, completion: Completion?) { | |
guard let controller = controller as? UIViewController else { return } | |
present(controller, animated: flag, completion: completion) | |
} | |
func dismissController(animated flag: Bool = true, completion: Completion? = nil) { | |
dismiss(animated: flag, completion: completion) | |
} | |
} | |
extension UIViewController: Controller { | |
var navigationController: NavigationController? { return navigationController } | |
} | |
protocol NavigationController { | |
func pushViewController(_ viewController: UIViewController, animated: Bool) | |
func popViewController(animated: Bool) -> UIViewController? | |
} | |
extension UINavigationController: NavigationController { } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
@testable import HostApp | |
class MockController: Controller { | |
var navigationController: NavigationController? { return mockedNavigationController } | |
var mockedNavigationController = MockNavigationController() | |
private(set) var lastPresentedError: Error? | |
private(set) var lastPresentedWrappedController: Controller? | |
private(set) var lastPresentedController: Controller? | |
private(set) var dismissControllerWasCalled = false | |
func presentAlert(error: Error) { | |
lastPresentedError = error | |
} | |
func present(wrappedController controller: Controller, animated flag: Bool, completion: Completion?) { | |
lastPresentedWrappedController = controller | |
completion?() | |
} | |
func present(controller: Controller, animated flag: Bool, completion: Completion?) { | |
lastPresentedController = controller | |
completion?() | |
} | |
func dismissController(animated flag: Bool, completion: Completion?) { | |
dismissControllerWasCalled = true | |
completion?() | |
} | |
} | |
class MockNavigationController: NavigationController { | |
private(set) var lastPushedViewController: UIViewController? | |
private(set) var popViewControllerWasCalled = false | |
func pushViewController(_ viewController: UIViewController, animated: Bool) { | |
self.lastPushedViewController = viewController | |
} | |
func popViewController(animated: Bool) -> UIViewController? { | |
popViewControllerWasCalled = true | |
return nil | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment