Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 356
Nikita Ermolenko Avatar asked Nov 08 '22 02:11

Nikita Ermolenko


1 Answers

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

like image 140
Nikita Ermolenko Avatar answered Nov 15 '22 04:11

Nikita Ermolenko