UIControl 的 sendActions 在测试目标中不起作用

UIControl's sendActions doesn't work in Test target

我正在尝试测试当 UITextField 发送 editingChanged 事件时调用特殊处理程序。因此,为此,我通过 sendActions 方法模拟此事件。但它在测试目标中不起作用,在项目中一切正常(运行 模式 - 模拟器)。

我写了一个小例子:

class Strange {
    private let handler: () -> Void

    init(textField: UITextField, handler: @escaping () -> Void) {
        self.handler = handler
        textField.addTarget(self, action:#selector(textableValueChanged), for: .editingChanged)
    }

    @objc private func textableValueChanged() {
        handler()
    }
}

这里我想看到 "test" 打印,但是在 sendActions 事件之后不会调用此处理程序。我也尝试过 expectation 但它对我没有帮助。

func testStrangeBehaviour() {
    let expectation = self.expectation(description: "Bla-bla")
    s = Strange(textField: textfield1) {
        print("test")
        expectation.fulfill()
    }

    textfield1.sendActions(for: .editingChanged)
    waitForExpectations(timeout: 5, handler: nil)
}

我做错了什么?

在 Reactive Cocoa 库中找到有趣的类别。 它帮助到我! https://github.com/RACCommunity/Rex/blob/master/Tests/Helpers/UIControl%2BEnableSendActionsForControlEvents.swift