Giter VIP home page Giter VIP logo

mockuialertcontroller's Introduction

MockUIAlertController

Build Status Coverage Status CocoaPods Version Carthage compatible

MockUIAlertController lets you mock iOS alerts and action sheets for unit tests. It works for Swift as well as Objective-C.

(For old UIAlertView or UIActionSheet mocking, use MockUIAlertViewActionSheet.)

No actual alerts are presented. This means:

  • The workflow doesn't pause for an action to be selected
  • Tests are blazing fast.

For more discussion, see my blog post How to Test UIAlertControllers and Control Swizzling.

Writing Tests

What do I need to change in production code?

Nothing.

How do I test an alert controller?

  1. #import <MockUIAlertController/QCOMockAlertVerifier.h> (or add it to your Swift test target's bridging header)
  2. Instantiate a QCOMockAlertVerifier before the execution phase of the test.
  3. Invoke the code to create and present your alert or action sheet.

Information about the alert or action sheet is then available through the QCOMockAlertVerifier.

For example, here's a test verifying the title. sut is the system under test in the test fixture.

- (void)testShowAlert_AlertShouldHaveTitle {
    QCOMockAlertVerifier *alertVerifier = [[QCOMockAlertVerifier alloc] init];

    [sut showAlert]; // Whatever triggers the alert

    XCTAssertEqualObjects(alertVerifier.title, @"Title");
}
func testShowAlert_AlertShouldHaveTitle() {
    let alertVerifier = QCOMockAlertVerifier()

    sut.showAlert() // Whatever triggers the alert

    XCTAssertEqual(alertVerifier.title, "Title")
}

To guard against tests which accidentally present real alerts, I recommend placing the alert verifier in the test fixture with setUp/tearDown.

How can I invoke the block associated with a UIAlertAction?

Go through the steps above to present your alert or action sheet using QCOMockAlertController. Then call -executeActionForButtonWithTitle: on your QCOMockAlertVerifier with the button title. For example:

- (void)testShowAlert_ExecutingActionForOKButton_ShouldDoSomething {
    QCOMockAlertVerifier *alertVerifier = [[QCOMockAlertVerifier alloc] init];

    [sut showAlert];
    [alertVerifier executeActionForButtonWithTitle:@"OK"];

    // Now assert what you want
}
func testShowAlert_ExecutingActionForOKButton_ShouldDoSomething() {
    let alertVerifier = QCOMockAlertVerifier()

    sut.showAlert()
    alertVerifier.executeActionForButton(withTitle: "OK")

    // Now assert what you want
}

Can I see some examples?

See the sample apps. Run them on both phone & pad to see what they do, then read the ViewController tests.

Adding it to your project

CocoaPods

Add the following to your Podfile, changing "MyTests" to the name of your test target:

target 'MyTests' do
  inherit! :search_paths
  pod 'MockUIAlertController', '~> 2.0'
end

Carthage

Add the following to your Cartfile:

github "jonreid/MockUIAlertController" ~> 2.0

Building It Yourself

Make sure to take everything from Source/MockUIAlertController.

mockuialertcontroller's People

Contributors

jonreid avatar foulkesjohn avatar readmecritic avatar batesyuk avatar yas375 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.