iosswiftxctestxctestexpectation

UIControl's sendActions doesn't work in Test target


I'm trying to test that when UITextField sends editingChanged event special handler is invoked. So, for that purpose I simulate this event by sendActions method. But it doesn't work in Test target, all ok just in the project (Run mode - simulator).

I've written a small example:

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()
    }
}

Here I want to see "test" print, but this handler isn't invoked after sendActions event. I've also tried with expectation but it didn't help me.

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)
}

What am I doing wrong?


Solution

  • Found interesting category in Reactive Cocoa library. It helps me! https://github.com/RACCommunity/Rex/blob/master/Tests/Helpers/UIControl%2BEnableSendActionsForControlEvents.swift