【发布时间】:2017-06-26 00:09:00
【问题描述】:
我正在尝试测试当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()
}
}
在这里我想看到“测试”打印,但是在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)
}
我做错了什么?
【问题讨论】:
-
我认为解决方案是stackoverflow.com/a/39856918/2294228。为我工作。
标签: ios swift xctest xctestexpectation