All Projects → Kuniwak → UIKitTestable

Kuniwak / UIKitTestable

Licence: MIT license
UIKit becomes testable.

Programming Languages

swift
15916 projects
ruby
36898 projects - #4 most used programming language

UIKitTestable

Swift 5.0 compatible Carthage MIT license Build Status

UIKit becomes testable.

Usage

Testing with UINavigationController.pushViewController(_:animated:)

// 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.

Testing with UIViewController.present(_:animated:completion:)

// 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.

Documentations

You can re-generate the docs by ./tools/generate-docs on this repository.

Installation

Carthage

Add the following line to your Cartfile:

github "Kuniwak/UIKitTestable"

CocoaPods

Not supported yet. If you want to support CocoaPods, please send the patch.

License

MIT.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].