Kuniwak / UIKitTestable

UIKit becomes testable.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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.

About

UIKit becomes testable.

License:MIT License


Languages

Language:Swift 96.8%Language:Ruby 1.9%Language:Shell 0.7%Language:Objective-C 0.5%