Skip to content

Instantly share code, notes, and snippets.

@joemasilotti
Last active September 10, 2017 22:39
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joemasilotti/5dd13766d23df729e405bf865e6d176a to your computer and use it in GitHub Desktop.
Save joemasilotti/5dd13766d23df729e405bf865e6d176a to your computer and use it in GitHub Desktop.
Testing View Controllers in Swift 3.0
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 { }
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