UIControl's sendActions doesn't work in Test target

1.7k Views Asked by At

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?

1

There are 1 best solutions below

0
On BEST ANSWER