UIKit becomes testable.
// BEFORE
import UIKit
class MyViewController: UIViewController {
@IBAction func doSomething(_ sender: Any) {
// Checking the whether pushViewController was called or not is hard.
self.navigationController?.pushViewController(self)
}
}
// AFTER
import UIKit
import UIKitTestable
class MyViewController: UIViewController {
private let navigator: NavigatorProtocol
@IBAction func doSomething(_ sender: Any) {
// Easily inject a NavigatorStub or NavigatorSpy because they conform NavigatorProtocol.
self.navigator.push(viewController: self)
}
}
// MyViewControllerTests.swift
import XCTest
import UIKitTestable
class MyViewControllerTests: XCTestCase {
func testDoSomething() {
let navigatorSpy = NavigatorSpy()
// Inject the spy to verify how many time the .push was called.
let myViewController = MyViewController(navigatorSpy)
myViewController.doSomething(nil)
XCTAssertEqual(navigatorSpy.calledArgs.count, 1)
}
}
See other usages.
// BEFORE
import UIKit
class MyViewController: UIViewController {
@IBAction func doSomething(_ sender: Any) {
// Checking the whether present was called or not is hard.
self.present(anotherViewController)
}
}
// AFTER
import UIKit
import UIKitTestable
class MyViewController: UIViewController {
private let modalPresenter: ModalPresenterProtocol
@IBAction func doSomething(_ sender: Any) {
// Easily inject a ModalPresenterStub or ModalPresenterSpy because they conform ModalPresenterProtocol.
self.modalPresnter.present(viewController: anotherViewController)
}
}
// MyViewControllerTests.swift
import XCTest
import UIKitTestable
class MyViewControllerTests: XCTestCase {
func testDoSomething() {
let modalPresenterSpy = ModalPresenterSpy()
// Inject the spy to verify how many time the .present was called.
let myViewController = MyViewController(modalPresenterSpy)
myViewController.doSomething(nil)
XCTAssertEqual(modalPresenterSpy.calledArgs.count, 1)
}
}
See other usages.
You can re-generate the docs by ./tools/generate-docs
on this repository.
Add the following line to your Cartfile
:
github "Kuniwak/UIKitTestable"
Not supported yet. If you want to support CocoaPods, please send the patch.
MIT.