Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
967 views
in Technique[技术] by (71.8m points)

ios - How to test if a UIControlEvents has been fired

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...