4

I have a library implementing a custom UIControl with a method which would fire a .valueChanged event when called. I would like to test the method for that behavior.

My custom control:

class MyControl: UIControl {
    func fire() {
        sendActions(for: .valueChanged)
    }
}

And the test:

import XCTest

class ControlEventObserver: NSObject {
    var expectation: XCTestExpectation!

    init(expectation anExpectation: XCTestExpectation) {
        expectation = anExpectation
    }

    func observe() {
        expectation.fulfill()
    }
}

class Tests: XCTestCase {
    func test() {
        let myExpectation = expectation(description: "event fired")
        let observer = ControlEventObserver(expectation: myExpectation)
        let control = MyControl()
        control.addTarget(observer, action: #selector(ControlEventObserver.observe), for: .valueChanged)
        control.fire()
        waitForExpectations(timeout: 1) { error in
            XCTAssertNil(error)
        }
    }
}

The problem is the observe method never gets called so the expectation is not fulfilled.

The question is: how can we test for UIControlEvents like in this case? Perhaps we need to force the runloop somehow?

EDIT 1: Please note that since I am testing a library, my test target does not have any Host Application. The test above passes when the test target has a host application.

Thanh Pham
  • 2,021
  • 21
  • 30
  • 1
    this is weird I have copied and pasted your code in a project I created and the expectation is fullfiled, test passed successfully. – Wilson Oct 04 '16 at 13:32
  • Just realized that might because that I am testing a library. When I create another test target with a host application, the test above passes. @WilsonBalderrama – Thanh Pham Oct 04 '16 at 15:29

2 Answers2

14

Apple's documentation for UIControl states that:

When a control-specific event occurs, the control calls any associated action methods right away. Action methods are dispatched through the current UIApplication object, which finds an appropriate object to handle the message, following the responder chain if needed.

When sendActions(for:) is called on a UIControl, the control will call the UIApplication's sendAction(_:to:from:for:) to deliver the event to the registered target.

Since I am testing a library without any Host Application, there is no UIApplication object. Hence, the .valueChanged event is not dispatched and the observe method does not get called.

Thanh Pham
  • 2,021
  • 21
  • 30
  • Then how did you simulate the tap event in a library? – DàChún Sep 06 '18 at 07:51
  • @User9527 I ended up created a simple Host App for my test suit – Thanh Pham Sep 06 '18 at 16:04
  • Would you be able to show how this was done @ThanhPham? I've got a similar question about UIButton [here](https://stackoverflow.com/q/58519479/2547229) – Benjohn Oct 23 '19 at 09:24
  • 1
    @Benjohn I created an app target, and in the test target's General settings screen, I made the app target the Host Application of the test target. – Thanh Pham Oct 23 '19 at 20:33
0

You are declaring the observer object inside the test method. This means that as soon as the method completes it will be released from memory and hence is not called. Create a reference to the observer at class level in the Tests class as follows and it will work.

class Tests: XCTestCase {

    var observer: ControlEventObserver!
    func test() {
        let myExpectation = expectation(description: "event fired")
        self.observer = ControlEventObserver(expectation: myExpectation)
        let control = MyControl()
        control.addTarget(observer, action:#selector(ControlEventObserver.observe), for: .valueChanged)
        control.fire()
        waitForExpectations(timeout: 1) { error in
            XCTAssertNil(error)
        }
    }
}

You will also need the myExpectation & control to be declared in the same way else that won't be called either.

Jacob King
  • 6,025
  • 4
  • 27
  • 45
  • Thanks for your answer. I think the objects are not released because I don't create any auto release pool. Also, I am testing a library so the test target doesn't have any Host Application. I've revised my question to reflect that. – Thanh Pham Oct 04 '16 at 15:34