SHARE
TWEET

Untitled

a guest Aug 25th, 2019 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import UIKit
  2. import XCTest
  3.  
  4. /// Loads a ViewController to be tested
  5. final class ViewControllerTestingLoader<T: UIViewController> {
  6.  
  7.     // MARK: - Private Properties
  8.  
  9.     private var rootWindow: UIWindow!
  10.  
  11.     // MARK: - Public Functions
  12.  
  13.     /// Sets up a viewController to be loader
  14.     ///
  15.     /// - Parameter viewController: the controller to be loaded
  16.     func setup(withViewController viewController: T) {
  17.         rootWindow = UIWindow(frame: UIScreen.main.bounds)
  18.         rootWindow.isHidden = false
  19.         rootWindow.rootViewController = viewController
  20.         _ = viewController.view
  21.         viewController.viewWillAppear(false)
  22.         viewController.viewDidAppear(false)
  23.     }
  24.  
  25.     /// Removes the controller from the window hierachy
  26.     func tearDown() {
  27.         guard let rootWindow = rootWindow,
  28.             let rootViewController = rootWindow.rootViewController as? T else {
  29.                 XCTFail("tearDown() was called without setup() being called first")
  30.                 return
  31.         }
  32.         rootViewController.viewWillDisappear(false)
  33.         rootViewController.viewDidDisappear(false)
  34.         rootWindow.rootViewController = nil
  35.         rootWindow.isHidden = true
  36.         self.rootWindow = nil
  37.     }
  38.  
  39.     /*********************/
  40.     /****** EXAMPLE ******/
  41.     /*********************/
  42.     /*
  43.  
  44.      final class MyTest: XCTestCase {
  45.  
  46.          private var sut: MyController!
  47.          private var viewControllerTestingLoader: ViewControllerTestingLoader<MyController>!
  48.  
  49.          override func setUp() {
  50.              super.setUp()
  51.              let controller = MyController()
  52.              sut = controller
  53.              viewControllerTestingLoader = ViewControllerTestingLoader<MyController>()
  54.              viewControllerTestingLoader.setup(withViewController: sut)
  55.          }
  56.  
  57.          override func tearDown() {
  58.              sut = nil
  59.              viewControllerTestingLoader.tearDown()
  60.              viewControllerTestingLoader = nil
  61.              super.tearDown()
  62.          }
  63.  
  64.      }
  65.  
  66.      */
  67.  
  68. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top