Mole 1.0.2

Mole 1.0.2

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release May 2016
SPMSupports SPM

Maintained by Hilton Campbell.



Mole 1.0.2

  • By
  • Hilton Campbell

Mole

Xcode’s UI testing is black-box by design. This works around that.

Installation

Install with CocoaPods by adding the following to your Podfile:

use_frameworks!

pod 'Mole'

Be sure to add Mole to both your app and UI test targets. Then run:

pod install

Usage

Create a server class in your app target. Register methods you want your UI test target to be able to call:

import Mole

class AppServer {
    private let server = MoleServer()

    init() {
        server["upgraded"] = { _ in
            return ProductController.sharedController.upgraded
        }

        server["setUpgraded"] = { args in
            if let args = args as? [String: Bool], upgraded = args["upgraded"] {
                ProductController.sharedController.upgraded = upgraded
            }

            return nil
        }
    }
}

Then create a proxy class in your UI test target, with proxy methods that correspond to the methods in your server class:

import Mole

class AppProxy {
    private let client = MoleClient()

    func upgraded() -> Bool {
        return client.invokeMethod("upgraded") as? Bool ?? false
    }

    func setUpgraded(upgraded: Bool) {
        client.invokeMethod("setUpgraded", parameters: ["upgraded": upgraded])
    }
}

Now you can call methods on your proxy class from your UI tests:

class Tests: XCTestCase {
    func testUpgrade() {
        let appProxy = AppProxy()
        appProxy.setUpgraded(true)
        XCTAssertTrue(appProxy.upgraded())
    }
}

License

Mole is released under the MIT license. See LICENSE for details.